1

I am fetching an array of objects as following :

const data =
  [ { id: 0, company: 'nike',   items: [{ id: 0, name: 'caret',        price: 100}] } 
  , { id: 1, company: 'adidas', items: [{ id: 1, name: 'mobile phone', price: 300}] } 
  ] 

I want to filter that array of objects by checking the value of the property "name" inside the items array if it is "Caret" or not. If yes, I want to return the same array of objects with all its content but filtered. Which means, it will filter out the object whose item's name is not caret. How can I achieve this in javascript?!

6
  • Related: stackoverflow.com/questions/11922383/… Commented Sep 15, 2020 at 12:28
  • @Taplar Fixed now. Can you help pls?! Commented Sep 15, 2020 at 12:32
  • Alright, so what have you already tried? What part is causing you problems? Commented Sep 15, 2020 at 12:33
  • @Taplar data.filter(i => i.items.map(item=> item.name === "Caret") Commented Sep 15, 2020 at 12:35
  • K, so instead of using map(), take a look at includes(). developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 15, 2020 at 12:35

2 Answers 2

2

I suppose you are looking for that ?

const data =
  [{ id: 0, company: 'nike',   items: [{ id: 0, name: 'caret',        price: 100}]}
  ,{ id: 1, company: 'adidas', items: [{ id: 1, name: 'mobile phone', price: 300}]}
  ] 

const filterName = (arr, name)=>arr.filter(el=>el.items.some(x=>x.name==name))

onlyCaret = filterName(data,'caret')

console.log( onlyCaret[0].company )  // nike
console.log( onlyCaret )   // row 0 with  { ... company: 'nike' ..}
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

2 Comments

I suggest using some instead of find for this scenario. some returns true or false if a match is found. find returns the found element.
@3limin4t0r correct, array.some() method is much smarter to use, thanks for that hint! (why didn't I think about it!)
1

You need to use Array.filter and then loop through your items for a match

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

const data = [
    {
        id:0,
        company: "nike"
            items:[
                  name: "caret",
                  price:100
            ]
    },
    {
        id:1,
        company: "adidas"
            items:[
                name: "mobile phone",
                price: 300
            ]
    },
    ]

const filteredCompanies = data.filter((company)=>{
    company.items.forEach((item)=>{
        if(item.name === "nike") return true
    })
})

9 Comments

Thanks for your comment. Yet, I want to return the same array without the targeted object filtered.
@Saif what you mean by same array?
do you mean the array of data or the array of items?
I mean the array of data. @JamesRushford
This answer does not work using return true inside an forEach callback will only return from the forEach callback, not from the filter callback.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.