Home > Enterprise >  if value that i want to find is in object in array, i want to return true by javascript
if value that i want to find is in object in array, i want to return true by javascript

Time:10-27

if value that i want to find is in object in array, i want to return true by javascript

below is array data.

[
  {
    holdings: [
      { product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } },
      { product: { id: 2, prodName: '컴투스', prodCode: '078340' } }
    ]
  },
  {
    holdings: [ { product: { id: 1, prodName: 'LX세미콘', prodCode: '108320' } } ]
  }
]

if any prodCode of product in holdings is "108320", i want to return true.

i tried

CodePudding user response:

You can use 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));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related