简体   繁体   English

从数组对象的 arrays 创建新数组 arrays,具有特定值

[英]Create new array of arrays from arrays of objects of array , with specific value

I have following data and I want to make a new array of arrays like below with Javascript. I tried to do a loop twice but couldn't get ideal results.我有以下数据,我想用 Javascript 创建一个 arrays 的新数组,如下所示。我尝试循环两次但无法获得理想的结果。

data: (5) [{…}, {…}, {…}, {…}, {…}]
// above data contains below:

[{
  id: 1,
 productName: "Tacos",

 productData: 
 [ { status: 1, ready: 85, total: 262},
   { status: 2, ready: 59, total: 16},
  
   { status: 3, ready: 67, total: 168},

   { status: 4, ready: 42, total: 301},
   { status: 5, ready: 10, total: 266}, 
   { status: 6, ready: 56, total: 220}, 
   { status: 7, ready: 28, total: 173}]  },
{
 id: 2
,
 productName: "Poke",

 productData: 
 [ { status: 1, ready: 85, total: 300},
   { status: 2, ready: 59, total: 150},

   { status: 3, ready: 67, total: 93},
   { status: 4, ready: 42, total: 173},
   { status: 5, ready: 10, total: 266}, 
   { status: 6, ready: 56, total: 98}, 
   { status: 7, ready: 28, total: 121}]  },
 {
 id: 3

 productName: "Sandwich",

 productData: 
 [ { status: 1, ready: 85, total: 141},
   { status: 2, ready: 59, total: 230},

   { status: 3, ready: 67, total: 155},

   { status: 4, ready: 42, total: 167},
   { status: 5, ready: 10, total: 98}, 
   { status: 6, ready: 56, total: 145}, 
   { status: 7, ready: 28, total: 123}]  
 }, 
 {
 id: 4
 productName: “Burrito”,

 productData: 
 [ { status: 1, ready: 85, total: 45},
   { status: 2, ready: 59, total: 62},

   { status: 3, ready: 67, total: 77},

   { status: 4, ready: 42, total: 21},
   { status: 5, ready: 10, total: 33}, 
   { status: 6, ready: 56, total: 85}, 
   { status: 7, ready: 28, total: 35}]  
 }, 
 {
 id: 5

 productName: “Gyoza”,

 productData: 
 [ { status: 1, ready: 85, total: 88},
   { status: 2, ready: 59, total: 83},

   { status: 3, ready: 67, total: 103},

   { status: 4, ready: 42, total: 98},
   { status: 5, ready: 10, total: 99}, 
   { status: 6, ready: 56, total: 120}, 
   { status: 7, ready: 28, total: 96}]  
 }, 
]


Outcome should be like this.结果应该是这样的。 Array of arrays which has numbers from each array with same value of status in productData. arrays 的数组,其中每个数组的数字在 productData 中具有相同的状态值。

[[262, 300, 141, 45, 88], // All numbers, value of total from status: 1 from each array of productData
 [16, 150, 230, 62, 83],  // status: 2
 [168, 301, 93, 155, 77], // status: 3
 ...]

Thank you in advance.先感谢您。

You should use flatMap() in conjunction with reduce() .您应该将flatMap()reduce()结合使用。

 const input = [ { id: 1, productName: "Tacos", productData: [ { status: 1, ready: 85, total: 262 }, { status: 2, ready: 59, total: 16 }, { status: 3, ready: 67, total: 168 }, { status: 4, ready: 42, total: 301 }, { status: 5, ready: 10, total: 266 }, { status: 6, ready: 56, total: 220 }, { status: 7, ready: 28, total: 173 }, ], }, { id: 2, productName: "Poke", productData: [ { status: 1, ready: 85, total: 300 }, { status: 2, ready: 59, total: 150 }, { status: 3, ready: 67, total: 93 }, { status: 4, ready: 42, total: 173 }, { status: 5, ready: 10, total: 266 }, { status: 6, ready: 56, total: 98 }, { status: 7, ready: 28, total: 121 }, ], }, { id: 3, productName: "Sandwich", productData: [ { status: 1, ready: 85, total: 141 }, { status: 2, ready: 59, total: 230 }, { status: 3, ready: 67, total: 155 }, { status: 4, ready: 42, total: 167 }, { status: 5, ready: 10, total: 98 }, { status: 6, ready: 56, total: 145 }, { status: 7, ready: 28, total: 123 }, ], }, { id: 4, productName: "Burrito", productData: [ { status: 1, ready: 85, total: 45 }, { status: 2, ready: 59, total: 62 }, { status: 3, ready: 67, total: 77 }, { status: 4, ready: 42, total: 21 }, { status: 5, ready: 10, total: 33 }, { status: 6, ready: 56, total: 85 }, { status: 7, ready: 28, total: 35 }, ], }, { id: 5, productName: "Gyoza", productData: [ { status: 1, ready: 85, total: 88 }, { status: 2, ready: 59, total: 83 }, { status: 3, ready: 67, total: 103 }, { status: 4, ready: 42, total: 98 }, { status: 5, ready: 10, total: 99 }, { status: 6, ready: 56, total: 120 }, { status: 7, ready: 28, total: 96 }, ], }, ]; const output = input.flatMap((it) => it.productData).reduce((allStatus, curStatus) => { allStatus[curStatus.status]? allStatus[curStatus.status].push(curStatus.total): (allStatus[curStatus.status] = [curStatus.total]); return allStatus; }, {}); console.log(Object.values(output));

First we flatten the array, so we get one huge array containing all productData :首先我们展平数组,所以我们得到一个包含所有productData的巨大数组:

const flattened = input.flatMap((it) => it.productData);

And after that reduce() will use a JavaScript object with the status as a key to collect all the values with the same status.之后reduce()将使用status为 JavaScript object 作为键来收集具有相同状态的所有值。 We start off with an empty object and check for each element whether we have encountered that status already.我们从一个空的 object 开始,并检查每个元素是否已经遇到该状态。 If we have not, we create an array containing this element total value.如果还没有,我们将创建一个包含此元素total的数组。 In case we have we just push() another value to that array.如果我们有我们只是push()另一个值到那个数组。

Last but not least, we only need to get the values of the JavaScript object as we are not interested in the keys.最后但同样重要的是,我们只需要获取 JavaScript object 的值,因为我们对密钥不感兴趣。 This can be done using Object.values() .这可以使用Object.values()来完成。

// flatten array
const flattened = input.flatMap((it) => it.productData);

// reduce values based on status
const output = flattened.reduce((allStatus, curStatus) => {
  allStatus[curStatus.status]
    ? allStatus[curStatus.status].push(curStatus.total)
    : (allStatus[curStatus.status] = [curStatus.total]);
  return allStatus;
}, {});

// get values of JS object
console.log(Object.values(output));

This can be achieved by mapping the individual values as sub-arrays and then 'zipping' the result.这可以通过将各个值映射为子数组然后“压缩”结果来实现。

 const input = [{ id: 1, productName: "Tacos", productData: [{ status: 1, ready: 85, total: 262 }, { status: 2, ready: 59, total: 16 }, { status: 3, ready: 67, total: 168 }, { status: 4, ready: 42, total: 301 }, { status: 5, ready: 10, total: 266 }, { status: 6, ready: 56, total: 220 }, { status: 7, ready: 28, total: 173 },], }, { id: 2, productName: "Poke", productData: [{ status: 1, ready: 85, total: 300 }, { status: 2, ready: 59, total: 150 }, { status: 3, ready: 67, total: 93 }, { status: 4, ready: 42, total: 173 }, { status: 5, ready: 10, total: 266 }, { status: 6, ready: 56, total: 98 }, { status: 7, ready: 28, total: 121 },], }, { id: 3, productName: "Sandwich", productData: [{ status: 1, ready: 85, total: 141 }, { status: 2, ready: 59, total: 230 }, { status: 3, ready: 67, total: 155 }, { status: 4, ready: 42, total: 167 }, { status: 5, ready: 10, total: 98 }, { status: 6, ready: 56, total: 145 }, { status: 7, ready: 28, total: 123 },], }, { id: 4, productName: "Burrito", productData: [{ status: 1, ready: 85, total: 45 }, { status: 2, ready: 59, total: 62 }, { status: 3, ready: 67, total: 77 }, { status: 4, ready: 42, total: 21 }, { status: 5, ready: 10, total: 33 }, { status: 6, ready: 56, total: 85 }, { status: 7, ready: 28, total: 35 },], }, { id: 5, productName: "Gyoza", productData: [{ status: 1, ready: 85, total: 88 }, { status: 2, ready: 59, total: 83 }, { status: 3, ready: 67, total: 103 }, { status: 4, ready: 42, total: 98 }, { status: 5, ready: 10, total: 99 }, { status: 6, ready: 56, total: 120 }, { status: 7, ready: 28, total: 96 },], },]; // map single values const productTotals = input.map(({ productData }) => productData.map(({ total }) => total)); // zip const result = productTotals[0].map((_, i) => productTotals.map(ts => ts[i])) console.log(result)

see: From an array of objects, extract value of a property as array and Javascript equivalent of Python's zip function for further discussion on both operations.请参阅: 从对象数组中提取属性值作为数组Javascript 等效于 Python 的 zip function以进一步讨论这两个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM