繁体   English   中英

如何在另一个数组中检索具有值的数组?

[英]How to retrieve an array with value within another array?

我正在尝试使用 filter 方法检索在另一个数组中具有值的数组。 所以例如,从这个

[[], [{name:"shop", price: 48}],[]] 

对此

[{name:"shop", price: 48}]

从下面的 console.log 我得到{name:"shop", price: 48} ,但是当我返回v ,我得到了整个东西--> [[], [{name:"shop", price: 48}],[]]

我错过了什么?

 const a = [[], [{name:"shop", price: 48}],[]] const output = a.map((value) => { return value.filter((v) =>{ console.log(v) return v }) }) console.log(output);

你在找这个吗?

 const a = [[], [{name:"shop", price: 48}],[]] const [ output ] = a.filter(v => v.length); // will filter out empty arrays console.log(output);

我将子数组加入一个数组的解决方案:

const a = [[], [{name:"shop", price: 12}], [{name:"shop", price: 34}]];

let res = [];
a.forEach((aItem) => res = res.concat(aItem));

console.log(res);

输出

[ { name: 'shop', price: 12 }, { name: 'shop', price: 34 } ]

暂无
暂无

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

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