简体   繁体   中英

How get items in object which contains in nested array given value

How I have some nested array of object, And I tried to get matched items with some value which stored in the nested object in this object and there is have nested array also.

Eg:

my data:

const items = [
    {
      name: "A",
      data: {
        title: "B",
        subData: {
           val: "AA", 
           anotherData: {
               subC: [
                   {
                    name: "Data Item", 
                    fruits: ["Apple"]
                   }, 
                   {
                    name: "Data Item 2", 
                    fruits: ["Orange"]
                    }
                   ]
              }
        }
      } 
    },
    {
      name: "A",
      data: {
        title: "B",
        subData: {
           val: "AA", 
           anotherData: {
               subC: [
                   {
                    name: "Data Item", 
                    fruits: ["Apple"]
                   }, 
                   {
                    name: "Data Item 2", 
                    fruits: ["Orange"]
                    }
                   ]
              }
        }
      } 
    }
]

Here is my data I have a value of fruit eg: "Apple" , and I need to get all items which contain Apple fruits array

how to do that with the es6 map or filter function?.

I tried this solution but getting undefined

 const v =  items.map((item) => {
        return item;
      }).map((a) => {
        return a.data.subData.anotherData
      }).map((b) => {
      console.log("b", b)
         return b
       }).map((x) => {
      return  x
    }).filter((o) => {
      return o.fruits.contains["Apple"]
    });

We can use Array.filter and Array.some

We filter items by checking if the subC array contains some data items containing the desired search item.

 const items = [ { name: "A", data: { title: "B", subData: { val: "AA", anotherData: { subC: [ { name: "Data Item", fruits: ["Apple"] }, { name: "Data Item 2", fruits: ["Orange"] } ] } } } }, { name: "A", data: { title: "B", subData: { val: "AA", anotherData: { subC: [ { name: "Data Item", fruits: ["Apple"] }, { name: "Data Item 2", fruits: ["Orange"] } ] } } } } ] function findItems(searchItem) { return items.filter(item => { return item.data.subData.anotherData.subC.some(di => di.fruits.includes(searchItem)); }) } console.log("Items containing Apple:", findItems("Apple")) // Or Oranges console.log("Items containing Orange:", findItems("Orange"))

Filter the array for an item that has the nested sub-state with a fruits array that includes "apple". This is a case-insensitive search.

items.filter((item) =>
  item.data.subData.anotherData.subC.some((el) =>
    el.fruits.some((fruit) => fruit.toLowerCase() === "apple")
  )
);

 const items = [ { name: "A", data: { title: "B", subData: { val: "AA", anotherData: { subC: [ { name: "Data Item", fruits: ["Apple"] }, { name: "Data Item 2", fruits: ["Orange"] } ] } } } }, { name: "A", data: { title: "B", subData: { val: "AA", anotherData: { subC: [ { name: "Data Item", fruits: ["Apple"] }, { name: "Data Item 2", fruits: ["Orange"] } ] } } } } ]; const result = items.filter((item) => item.data.subData.anotherData.subC.some((el) => el.fruits.some((fruit) => fruit.toLowerCase() === "apple") ) ); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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