创建来自数组对象数组的新数组,具有特定值

发布于 2025-01-23 14:28:01 字数 3016 浏览 0 评论 0 原文

我有以下数据,我想使用JavaScript制作一个新数组。 我试图两次进行循环,但无法获得理想的结果。

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}]  
 }, 
]


结果应该是这样。 来自每个数组中的数字数组数组中具有相同状态数据的数字。

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

先感谢您。

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2025-01-30 14:28:01

您应该使用 /code> 降低()

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));

首先,我们将数组弄平,因此我们获得一个包含所有 productData 的巨大数组:

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

然后在此之后 redair()将使用 status status 作为收集具有相同状态的所有值的钥匙。
我们从一个空对象开始,并检查每个元素是否已经遇到了该状态。如果没有,我们创建一个包含此元素的数组 total 值。如果我们只有 push()该数组的另一个值。

最后但并非最不重要的一点是,我们只需要获取JavaScript对象的值,因为我们对密钥不感兴趣。可以使用 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));

You should use flatMap() in conjunction with 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:

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.
We start off with an empty object and check for each element whether we have encountered that status already. If we have not, we create an array containing this element total value. In case we have we just push() another value to that array.

Last but not least, we only need to get the values of the JavaScript object as we are not interested in the keys. This can be done using 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));
挽你眉间 2025-01-30 14:28:01

这可以通过将单个值映射为子阵列,然后将结果“缩放”来实现。

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)

请参阅:属性的提取值 javascript'> javascript a>以进一步讨论这两个操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文