[英]How to convert an object of just array values into an array of objects, each with transposed property values?
What is the most efficient way to convert the data below in node.js?在 node.js 中转换以下数据的最有效方法是什么? Any help would be much appreciated.
任何帮助将非常感激。 Thanks!
谢谢!
I have some data that looks like this ->我有一些数据看起来像这样 ->
const inputdata = {
data1: [5.0, 10.0, 50.0, 100.0, 500.0],
data2: [5.0, 10.0, 50.0, 100.0, 500.0],
data3: [5.0, 10.0, 50.0, 100.0, 500.0],
};
and I need to convert it into this ->我需要把它转换成这个 ->
[
{ data1: 5.0, data2: 5.0, data3: 5.0 },
{ data1: 10.0, data2: 10.0, data3: 10.0 },
{ data1: 50.0, data2: 50.0, data3: 50.0 },
{ data1: 100.0, data2: 100.0, data3: 100.0 },
{ data1: 500.0, data2: 500.0, data3: 500.0 },
]
Code attempt ->代码尝试 ->
const inputdata = {
data1: [5.0, 10.0, 50.0, 100.0, 500.0],
data2: [5.0, 10.0, 50.0, 100.0, 500.0],
data3: [5.0, 10.0, 50.0, 100.0, 500.0],
};
const outputdata = [];
Object.keys(inputdata).forEach(key => {
let value = inputdata[key];
if (key === "data1"){
for (let i = 0; i < value.length; i++) {
outputdata.push({ data1: 0, data2: 0, data3: 0 });
}
}
}
Object.keys(inputdata).forEach(key => {
let value = inputdata[key];
if (key === "data1") {
for (let i = 0; i < value.length; i++) {
outputdata[i].data1 = value[i];
}
}
if (key === "data2") {
for (let i = 0; i < value.length; i++) {
outputdata[i].data2 = value[i];
}
}
if (key === "data3") {
for (let i = 0; i < value.length; i++) {
outputdata[i].data3 = value[i];
}
}
}
console.log(inputdata);
console.log(outputdata);
You could transpose the data and build new objects from data.您可以转置数据并从数据构建新对象。
const data = { data1: [5, 10, 50, 100, 500], data2: [5, 10, 50, 100, 500], data3: [5, 10, 50, 100, 500] }, result = Object.entries(data).reduce((r, [k, a]) => a.map((v, i) => ({...r[i], [k]: v })), []); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
Used techniques of the below provided step by step example code...下面提供的分步示例代码使用的技术...
// reducer function which transposes // a table into a matrix and vice versa. function transpose(result, row) { return row.reduce((matrix, value, idx) => { (matrix[idx]??= []).push(value); return matrix; }, result); } const inputdata = { data1: [5.0, 10.0, 50.0, 100.0, 500.0], data2: [5.0, 10.0, 50.0, 100.0, 500.0], data3: [5.0, 10.0, 50.0, 100.0, 500.0] }; console.log( Object // convert `inputdata` into a table //... an array of row like arrays. .values(inputdata) ); console.log( Object // - convert `inputdata` into a table //... an array of row like arrays. .values(inputdata) // - then transpose the table into a matrix. .reduce(transpose, []) ); console.log( Object // - convert `inputdata` into a table //... an array of row like arrays. .values(inputdata) // - then transpose the table into a matrix. .reduce(transpose, []) // - then convert/map matrix // into an array of objects. .map(vector => vector.reduce((object, value, idx) => Object.assign(object, { [`data${ idx + 1 }`]: value, }), {} ) ) );
.as-console-wrapper { min-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.