Lance
Lance

Reputation: 79

JS Filter and Map

I am trying to take a JSON response, filter it to find a specific matching group and if it matches, return a single key value.

My current attempt is:

let res = item.filter(it => it.groups.includes('unknown') && it.iccid);
    console.log(res.it.iccid)

But if my response has five items that are apart of the same group, how to I cycle through to output each?

I'd like to use item.map((it) => { return it['itemhere'] but unsure how to filter to get matching groups only...

Upvotes: 0

Views: 47

Answers (1)

selfagency
selfagency

Reputation: 1578

If I'm following you correctly, you want to figure out if the item's group array contains the term unknown and if so, to return the iccid of the item.

One way to do it would be:

let items = [{ ...your array of objects... }]

items = items.filter(item => {
  const unknown = item.groups.filter(i => i.includes('unknown'))
  if (unknown.length && item.iccid) { return item }
})

items.forEach(item => console.log(item))

Upvotes: 2

Related Questions