0

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"]
    });
2
  • 1
    Can you, given the provided sample data, also provide an example expected output? What do you want returned when an array containing "apple" is found? Commented Nov 18, 2020 at 8:03
  • I want to return the root item from this. soo root items which matched with "Apple" in fruits. Commented Nov 18, 2020 at 8:10

2 Answers 2

1

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"))

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

2 Comments

It's a very nice approach, but I want to return the root item from this. soo root items which matched with "Apple" in fruits. it's given to me subC items only
It's a very beautiful solution, you help me a lot. Thank you very much!
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.