我想从表中制作特定的json。我有一个表,有行和4列。 Here is my table我想从表中构建一个jsonarray。 左列中的第一个值是json的键,右列中的最后一个值是json的值。
我的意思是我想从表jsonarray获取,它必须看起来像
json_from_form = [{color: 'id',
name: "mouse",
x: "table",
y: "book"}];
我曾尝试构建json,但是在结构和在json对象中设置键时遇到问题。 请帮我修改json对象的正确结构。
var json_from_form_tmp = {};
$('#table').find('tbody tr').each(function (i) {
//var name = $(this).find('td:first').text();
json_from_form_tmp[i] = {
imd: $(this).find('td:eq(3) input').val()
};
});
console.log(json_from_form_tmp);
答案 0 :(得分:0)
要设置对象的属性(json_from_form_tmp),请使用['propertyName']表示法。
//get the name of the property from the first column
var name = $(this).find('td:first').text();
//use that name as the name of the property. Your value fetch was right!
json_from_form_tmp[name] = $(this).find('td:eq(3) input').val();
这是一个微小修改的小提琴。
答案 1 :(得分:0)
你应该使用jQuery map-function,这是一个例子:
$(function () {
var m = $("table tr").map(function (index, e) {
return {
color: $(e).children().eq(0).text(),
name: $(e).children().eq(1).text()
}
}).get();
});
其中m将是map函数内定义的对象数组。