I have an object with section/fields as below:
{
"section-1": {
"fields": [
{"id": "field1", value: "Field1"},
{"id": "field2", value: "Field2"}
]
},
"section-2": {
"fields": [
{"id": "field3", value: "Field3"},
{"id": "field4", value: "Field4"}
]
},
"section-3": {
"fields": [
{"id": "field5", value: "Field5"},
{"id": "field6", value: "Field6"}
]
}
}
Now there is a dynamic array which I get as below:
['field1', 'field5']
What I want to is iterate over the above array items (i.e. 'field1'
and 'field5'
) and find the corresponding section inside which the field id is present.
So for the above example, I want the output as below:
['section-1', 'section-3']
Is there any ES6 way to do the same?
3
Answers
Edit: This kind of questions are best answered by generative AI.
Here is the answer by Codeium
The one provided by SO gave: "
Unfortunately, we aren’t able to locate any information about this topic on Stack Overflow"
filter()
theObject.keys()
where you check ifsome()
of thefield.id
includes()
in yourto-find-array
.You can do this by using
Object.keys
andfind
methods in combination withsome
array iteration