数组数组到 Objects 数组并返回
Array of Arrays to Array of Objects and back
我需要一种通用方法来获取(数组的数组)+(header 数组)并将其转换为 JSON 的 object 数组,然后将其转换回来到数组的数组。 http://www.thomasfrank.se/SQLike/ 的 SQLike 库中的 'unpack'/'pack' 方法执行此操作。由于各种原因,我真的不喜欢这个库,更愿意使用其他东西。谁能推荐一个解决方案?
澄清一下,我想在这两个数据之间来回切换'structures':
1) var headers = ['col_1', 'col_2']
var data = [['x0', 0.34],['x1', 0.35]]
2) var objArr = [[{col_1: 'x0'},{col_2: 0.34}], [{col_1: 'x1', col_2: 0.35}]]
我无法每次都创建object模板,因为我正在从google张中读取各种数据。因此,我需要某种通用方法来执行此操作。
您可以只使用 named 数组从数组中的数据创建 objects 中的属性:
function toObjects(headers, data) {
var result = [];
for (var i = 0; i < data.length; i++) {
var o = {};
for (var j = 0; j < headers.length; j++) {
o[headers[j]] = data[i][j];
}
result.push(o);
}
return result;
}
然后使用headers从objects读取属性并放入数组:
function toArray(headers, data) {
var result = [];
for (var i = 0; i < data.length; i++) {
var a = [];
for (var j = 0; j < headers.length; j++) {
a.push(data[i][headers[j]]);
}
result.push(a);
}
return result;
}
这应该有效。请注意,正如 Guffa 在评论中提到的那样,pack 数组中值的顺序,即生成 header 数组的顺序,不能保证与解包时相同,但 headers 和值匹配。
function unpack(headers, values) {
var result = [];
for(var v = 0; v < values.length; v++) {
var arr = values[v];
var obj = {};
for(var i = 0; i < headers.length; i++) {
obj[headers[i]] = arr[i];
}
result.push(obj);
}
return result;
}
function pack(values) {
var result = [];
var headers = null;
for(var v = 0; v < values.length; v++) {
var obj = values[v];
if(headers == null) {
headers = [];
for(var p in obj) {
headers.push(p);
}
}
var arr = [];
for(var i = 0; i < headers.length; i++) {
arr[i] = obj[headers[i]];
}
result.push(arr);
}
return { headers: headers, data: result };
}
var headers = ['col_1', 'col_2'] ;
var data = [['x0', 0.34],['x1', 0.35]];
var unpacked = unpack(headers, data);
console.log(JSON.stringify(unpacked));
var objArr = [{col_1: 'x0', col_2: 0.34}, {col_1: 'x1', col_2: 0.35}];
var packed = pack(objArr);
console.log(JSON.stringify(packed));
http://jsfiddle.net/ryanwheale/tp3nk4zf/2/
var headers = ['col_1', 'col_2'];
var data = [['x0', 0.34], ['x1', 0.35]];
function unpack(headers, data) {
var unpacked = [], obj, i, il, n, nl;
for (i = 0, il = data.length; i < il; i++) {
obj = {};
for (n = 0, nl = headers.length; n < nl; n++) {
obj[headers[n]] = data[i][n];
}
unpacked.push([obj]);
}
return unpacked;
}
function pack(headers, data) {
var packed = [], arr, i, il, n, nl;
for (i = 0, il = data.length; i < il; i++) {
arr = [];
for (n = 0, nl = headers.length; n < nl; n++) {
arr.push(data[i][0][ headers[n] ]);
}
packed.push(arr);
}
return packed;
}
var unpacked = unpack(headers, data);
var packed = pack(headers, unpacked);
console.log(unpacked, packed);
这应该可以很好地完成工作 =)
var pack = function(headers, data) {
return data.map(function(row) {
return row.map(function(column, i) {
return Object.defineProperty({}, headers[i], {value: column});
});
});
};
var unpack = function (packed) {
var headers = [];
return {
data: packed.map(function(row) {
return row.map(function(column, i) {
var key = Object.keys(column)[0];
if (headers.length !== row.length) {
headers.push(key);
}
return column[key];
});
}),
headers: headers,
};
};
console.log('pack', pack(['col_1', 'col_2'], [['x0', 0.34], ['x1', 0.35]]));
console.log('unpack', unpack([[{col_1: 'x0'}, {col_2: 0.34}], [{col_1: 'x1'}, {col_2: 0.35}]]));
我需要一种通用方法来获取(数组的数组)+(header 数组)并将其转换为 JSON 的 object 数组,然后将其转换回来到数组的数组。 http://www.thomasfrank.se/SQLike/ 的 SQLike 库中的 'unpack'/'pack' 方法执行此操作。由于各种原因,我真的不喜欢这个库,更愿意使用其他东西。谁能推荐一个解决方案?
澄清一下,我想在这两个数据之间来回切换'structures':
1) var headers = ['col_1', 'col_2']
var data = [['x0', 0.34],['x1', 0.35]]
2) var objArr = [[{col_1: 'x0'},{col_2: 0.34}], [{col_1: 'x1', col_2: 0.35}]]
我无法每次都创建object模板,因为我正在从google张中读取各种数据。因此,我需要某种通用方法来执行此操作。
您可以只使用 named 数组从数组中的数据创建 objects 中的属性:
function toObjects(headers, data) {
var result = [];
for (var i = 0; i < data.length; i++) {
var o = {};
for (var j = 0; j < headers.length; j++) {
o[headers[j]] = data[i][j];
}
result.push(o);
}
return result;
}
然后使用headers从objects读取属性并放入数组:
function toArray(headers, data) {
var result = [];
for (var i = 0; i < data.length; i++) {
var a = [];
for (var j = 0; j < headers.length; j++) {
a.push(data[i][headers[j]]);
}
result.push(a);
}
return result;
}
这应该有效。请注意,正如 Guffa 在评论中提到的那样,pack 数组中值的顺序,即生成 header 数组的顺序,不能保证与解包时相同,但 headers 和值匹配。
function unpack(headers, values) {
var result = [];
for(var v = 0; v < values.length; v++) {
var arr = values[v];
var obj = {};
for(var i = 0; i < headers.length; i++) {
obj[headers[i]] = arr[i];
}
result.push(obj);
}
return result;
}
function pack(values) {
var result = [];
var headers = null;
for(var v = 0; v < values.length; v++) {
var obj = values[v];
if(headers == null) {
headers = [];
for(var p in obj) {
headers.push(p);
}
}
var arr = [];
for(var i = 0; i < headers.length; i++) {
arr[i] = obj[headers[i]];
}
result.push(arr);
}
return { headers: headers, data: result };
}
var headers = ['col_1', 'col_2'] ;
var data = [['x0', 0.34],['x1', 0.35]];
var unpacked = unpack(headers, data);
console.log(JSON.stringify(unpacked));
var objArr = [{col_1: 'x0', col_2: 0.34}, {col_1: 'x1', col_2: 0.35}];
var packed = pack(objArr);
console.log(JSON.stringify(packed));
http://jsfiddle.net/ryanwheale/tp3nk4zf/2/
var headers = ['col_1', 'col_2'];
var data = [['x0', 0.34], ['x1', 0.35]];
function unpack(headers, data) {
var unpacked = [], obj, i, il, n, nl;
for (i = 0, il = data.length; i < il; i++) {
obj = {};
for (n = 0, nl = headers.length; n < nl; n++) {
obj[headers[n]] = data[i][n];
}
unpacked.push([obj]);
}
return unpacked;
}
function pack(headers, data) {
var packed = [], arr, i, il, n, nl;
for (i = 0, il = data.length; i < il; i++) {
arr = [];
for (n = 0, nl = headers.length; n < nl; n++) {
arr.push(data[i][0][ headers[n] ]);
}
packed.push(arr);
}
return packed;
}
var unpacked = unpack(headers, data);
var packed = pack(headers, unpacked);
console.log(unpacked, packed);
这应该可以很好地完成工作 =)
var pack = function(headers, data) {
return data.map(function(row) {
return row.map(function(column, i) {
return Object.defineProperty({}, headers[i], {value: column});
});
});
};
var unpack = function (packed) {
var headers = [];
return {
data: packed.map(function(row) {
return row.map(function(column, i) {
var key = Object.keys(column)[0];
if (headers.length !== row.length) {
headers.push(key);
}
return column[key];
});
}),
headers: headers,
};
};
console.log('pack', pack(['col_1', 'col_2'], [['x0', 0.34], ['x1', 0.35]]));
console.log('unpack', unpack([[{col_1: 'x0'}, {col_2: 0.34}], [{col_1: 'x1'}, {col_2: 0.35}]]));