如果我要查找的值在对象数组中,我希望javascript返回true
下面是数组数据
[
{
holdings: [
{ product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } },
{ product: { id: 2, prodName: '컴투스', prodCode: '078340' } }
]
},
{
holdings: [ { product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } } ]
}
]
如果产品的produccode为"108320",我想返回true。
我试着
您可以使用Array.prototype.some
:
const data = [
{
holdings: [
{ product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } },
{ product: { id: 2, prodName: '컴투스', prodCode: '078340' } }
]
},
{
holdings: [ { product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } } ]
}
]
function present(code, data) {
return data.some(item =>
item.holdings.some(holding =>
holding.product.prodCode === code
)
)
}
console.log(present('108320', data));