I'm trying to extract objects that are within an array whose name
property includes
a keyword. I can do this by iterating through my array but I wanted to see if there's a more efficient way to do this.
Here's what my array and objects within it look like:
[
{
"accountId": 123,
"items": [
{
"id": 1,
"name": "Property XYZ"
},
{
"id": 2,
"name": "Gadget 1"
}
]
},
{
"accountId": 234,
"items": [
{
"id": 7,
"name": "Property ABC"
},
{
"id": 8,
"name": "Property QWERTY"
}
]
}
]
There are two objectives, one a bit simpler:
- I'd like to return only those objects whose
name
propertyincludes
the keyword "Property" - Same criteria as above but I'd like to include return the parent object that contains the object I'm looking for.
So, the output for the first requirement would look like this:
[
{
"id": 1,
"name": "Property XYZ"
},
{
"id": 7,
"name": "Property ABC"
},
{
"id": 8,
"name": "Property QWERTY"
}
]
And the output for the second requirement would look like this:
[
{
"accountId": 123,
"items": [
{
"id": 1,
"name": "Property XYZ"
}
]
},
{
"accountId": 234,
"items": [
{
"id": 7,
"name": "Property ABC"
},
{
"id": 8,
"name": "Property QWERTY"
}
]
}
]
Is there another way than iterating through the whole array and pushing copies of objects that I'm looking for into a new array?