Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要帮助从深层数组中提取对象并合并到单个数组中吗_Javascript_Node.js - Fatal编程技术网

Javascript 需要帮助从深层数组中提取对象并合并到单个数组中吗

Javascript 需要帮助从深层数组中提取对象并合并到单个数组中吗,javascript,node.js,Javascript,Node.js,提取具有target属性的对象并将其组合到一个数组中的最佳方法是什么 app.js const p1 = [[{ target1: 3 }, { target2: 1 }], [{ t: 2 }]]; const p2 = [[{ target1: 1 }, { target2: 2 }], [{ t: 2 }]]; const p3 = [[{ target1: 4 }, { target2: 1 }], [{ t: 2 }]]; const p4 = [[{ target1: 2 }, {

提取具有
target
属性的对象并将其组合到一个数组中的最佳方法是什么

app.js

const p1 = [[{ target1: 3 }, { target2: 1 }], [{ t: 2 }]];
const p2 = [[{ target1: 1 }, { target2: 2 }], [{ t: 2 }]];
const p3 = [[{ target1: 4 }, { target2: 1 }], [{ t: 2 }]];
const p4 = [[{ target1: 2 }, { target2: 2 }], [{ t: 2 }]];


Promise.all([p1, p2, p3, p4]).then((values) => {
    console.log(values);
    // Some way to extract the objects that have the target property in them 
    // and combine them into a single array
});
[ [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ] ]
结果

const p1 = [[{ target1: 3 }, { target2: 1 }], [{ t: 2 }]];
const p2 = [[{ target1: 1 }, { target2: 2 }], [{ t: 2 }]];
const p3 = [[{ target1: 4 }, { target2: 1 }], [{ t: 2 }]];
const p4 = [[{ target1: 2 }, { target2: 2 }], [{ t: 2 }]];


Promise.all([p1, p2, p3, p4]).then((values) => {
    console.log(values);
    // Some way to extract the objects that have the target property in them 
    // and combine them into a single array
});
[ [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ],
  [ [ [Object], [Object] ], [ [Object] ] ] ]
所需的

[{ target1: 3 }, { target2: 1 },
{ target1: 1 }, { target2: 2 },
{ target1: 4 }, { target2: 1 },
{ target1: 2 }, { target2: 2 }]

您可以使用下面的代码实现您的目标

const p1 = [[{ target1: 3 }, { target2: 1 }], [{ t: 2 }]];
const p2 = [[{ target1: 1 }, { target2: 2 }], [{ t: 2 }]];
const p3 = [[{ target1: 4 }, { target2: 1 }], [{ t: 2 }]];
const p4 = [[{ target1: 2 }, { target2: 2 }], [{ t: 2 }]];

Promise.all([p1, p2, p3, p4]).then((values) => {
   const [a1, a2, a3, a4] = values

   const [t1] = a1;
   const [t2] = a2;
   const [t3] = a3;
   const [t4] = a4;

   const result = [...t1, ...t2, ...t3, ...t4];

   console.log(result);
});

尝试在阵列上映射并使用解构:

let transformedValues = Array.prototype.concat( //poor man's one level flatten when combined with ...
  ...values.map(([[target1, target2]])=> [target1, target2])
)

我假设
p
值的数量不同,但是
p
值的结构是恒定的。

您好,欢迎使用堆栈溢出!请拿着这本书,通读一遍,特别是你的,关于SO的相关主题,并试一试。如果你在做了更多的研究和搜索后陷入困境,无法摆脱困境,请发布一份你的尝试,并明确指出你陷入困境的地方。人们会乐意帮忙的。祝你好运小的解释和代码片段会让你的问题变得更好:)