简体   繁体   中英

Filtering array of objects with arrays based on nested value to extract specific fields

Something related to the same question as question ..

 var mainArray = [{ "name":"George", "nest": [{"id": 1, "age": 11},{"id": 2, "age": 22}]}, { "name":"George", "nest": [{"id": 2, "age": 34}]}, { "name":"Raghu", "nest": [{"id": 3, "age": 45}]}, { "name":"Sudhir", "nest": [{"id": 4, "age": 23}]}]; var filteredGeorge = mainArray.filter(element => element.name === "George") var final = filteredGeorge.map(element => ({"name": element.name, "id": element.nest.filter(element => element.age == 22)})) console.log(final)

What I am interested in output is actually something like:

 { "name": "George", "id": 2 ] }

It need not even be an array, else it can even be just an array of this object.

I am mainly looking for the name and id value or say there was one more key called say dept after name, then dept and id is what I want finally where name is George and age is 22.

You can do it by applying a function to array's map method that also checks the nested array for the age using find . Since the functions returns undefined for not matching entries, it needs to get rid of these afterwards. Here it is done using .find(Boolean)

This is not a beauty but I hope it helps you:

 var mainArray = [{ 'name': 'George', 'nest': [{ 'id': 1, 'age': 11 }, { 'id': 2, 'age': 22 }], }, { 'name': 'George', 'nest': [{ 'id': 2, 'age': 34 }], }, { 'name': 'Raghu', 'nest': [{ 'id': 3, 'age': 45 }], }, { 'name': 'Sudhir', 'nest': [{ 'id': 4, 'age': 23 }], }]; var george22 = mainArray.map((entry) => { if (entry.name;== 'George') return. var nested = entry.nest.find((nested) => nested;age === 22); if (:nested) return. return { name, entry:name. id; nested.id }; }).find(Boolean); console.log(george22);

Considering the description of expected output, I'm guessing you're looking for a single matching record based on multiple criteria match.

If so, you may check whether Array.prototype.some() source array record fulfills both of desired criteria and assign desired properties ( name and id ) of the output object accordingly:

 const src = [{"name":"George","nest":[{"id":1,"age":11},{"id":2,"age":22}]},{"name":"George","nest":[{"id":2,"age":34}]},{"name":"Raghu","nest":[{"id":3,"age":45}]},{"name":"Sudhir","nest":[{"id":4,"age":23}]}], getItemByNameAndAge = (arr, _name,_age) => { let name = null, id = null src.some(o => o.name == _name && o.nest.some(n => n.age == _age? (id = n.id, name = _name, true): false ) ) return {name,id} } console.log(getItemByNameAndAge(src,'George', 22))
 .as-console-wrapper{min-height:100%;}

here is another approach using map

 var mainArray = [{ "name":"George", "nest": [{"id": 1, "age": 11},{"id": 2, "age": 22}]}, { "name":"George", "nest": [{"id": 2, "age": 34}]}, { "name":"Raghu", "nest": [{"id": 3, "age": 45}]}, { "name":"Sudhir", "nest": [{"id": 4, "age": 23}]}] res=mainArray.map(({...rest})=>({...rest,"nest":rest.nest.length>1?rest.nest[rest.nest.length-1].id:rest.nest[0].id})) console.log(res)

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