3

I have an array of object, in which each object has an inner array of the object which in turn has again an inner array of objects, it can go to any level of the inner object which I want to filter and display.

obj = [{ name: "apple", subGroups: [{ name: "apple-a", subGroups: { name: "apple-b", subGroups: [{ name: "apple-c", subGroups: { name: "apple-d", subGroups:[]}}]}}]}
{ name: "orange", subGroups: [{ name: "orange-a", subGroups: { name: "orange-b", subGroups: [{ name: "orange-c", subGroups: { name: "orange-d", subGroups:[]}}]}}]}
{ name: "mango", subGroups: [{ name: "123", subGroups: []}]}
{ name: "grapes", subGroups: [{ name: "123", subGroups: []}]}
{ name: "pear", subGroups: [{ name: "123", subGroups: []}]}]

searchvalue is dynamic 'orange-d' ,

searchvalue = 'orange-d';
result = { name: "orange-d", subGroups:[]}
 const newArr = obj.map(obj => {
 return obj.name === searchvalue;
 }).flat();

I have tried with filter, map, flat but I was not able to find the output, pls help

Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
asked Sep 29, 2020 at 18:13
2
  • 1
    please add the wanted result. why is subGroups an array or object? Commented Sep 29, 2020 at 18:24
  • do you want only a single object as result? Commented Sep 29, 2020 at 18:41

2 Answers 2

4

You could take Array#flatMap with a previous check for the handed over array or object, as if follows later as subGroups.

const
 find = (array, name) => (Array.isArray(array) ? array : [array])
 .flatMap(o => o.name === name ? o : find(o.subGroups, name)),
 data = [{ name: "apple", subGroups: [{ name: "apple-a", subGroups: { name: "apple-b", subGroups: [{ name: "apple-c", subGroups: { name: "apple-d", subGroups: [] } }] } }] }, { name: "orange", subGroups: [{ name: "orange-a", subGroups: { name: "orange-b", subGroups: [{ name: "orange-c", subGroups: { name: "orange-d", subGroups: [] } }] } }] }, { name: "mango", subGroups: [{ name: "123", subGroups: [] }] }, { name: "grapes", subGroups: [{ name: "123", subGroups: [] }] }, { name: "pear", subGroups: [{ name: "123", subGroups: [] }] }],
 result = find(data, 'orange-d');
console.log(result);

answered Sep 29, 2020 at 19:02
Sign up to request clarification or add additional context in comments.

Comments

3

side note: it would probably be better if you will reconsider that recursive structure. for the solution you should use some kind of recursion like so:

var obj = [
 { name: "apple", subGroups: [
 { name: "apple-a", subGroups: [
 { name: "apple-b", subGroups: [
 { name: "apple-c", subGroups: [
 { name: "apple-d", subGroups:[]}
 ]}
 ]}
 ]}
 ]},
 { name: "orange", subGroups: [
 { name: "orange-a", subGroups: [
 { name: "orange-b", subGroups: [
 { name: "orange-c", subGroups: [
 { name: "orange-d", subGroups:[]}
 ]}
 ]}
 ]}
 ]},
 { name: "mango", subGroups: [
 { name: "123", subGroups: []}
 ]},
 { name: "grapes", subGroups: [
 { name: "123", subGroups: []}
 ]},
 { name: "pear", subGroups: [
 { name: "123", subGroups: []}
 ]}
];
var searchvalue = 'orange-d';
function findName(array, name) {
 let result = {};
 
 if(!(Array.isArray(array) && array.length))
 return result;
 
 for(let item of array) {
 if(item.name === name) return item;
 
 result = findName(item.subGroups, name);
 
 if (result && result.name === name) return result;
 }
 
 return result;
}
console.log(findName(obj, searchvalue));

answered Sep 29, 2020 at 18:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.