简体   繁体   English

如何将仅包含数组值的 object 转换为对象数组,每个对象都具有转置属性值?

[英]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.

相关问题 将具有相同属性的对象数组转换为具有数组值的一个对象 - Convert array of objects with same property to one object with array values 对象数组到属性值的对象 - Array of objects to object of property values 如何将具有数组作为值的对象转换为对象数组 - How to convert an object with arrays as values to an array of objects 将值数组转换为具有该值作为属性的对象数组 - Convert array of values into array of objects with that value as a property 如何按照包含每个对象中存在的属性的唯一值的另一个数组的顺序对对象数组进行排序? - How to sort an array of objects in the order of another array containing the unique values of a property present in each object? 将对象数组转换为 object,其属性值按键名分组 - Convert array of objects into object with property values grouped by key name 如何将值转换为对象数组 - How to convert values to array of objects 给定一个json对象数组,如何将每个对象中的值转换为int或float? - Given an array of json objects, how do I convert values in each object to an int or float? 访问对象数组javascript中的对象属性值 - Access object property values in an array of objects javascript 如何将每个 object 具有元素数组的对象列表转换为以子元素作为属性的对象数组 - How to convert a list of objects with each object having an array of elements, to an array of objects with the subelement as a property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM