Suppose I have an array called arr1
and an object named arr2
containing a nested array called config
.
If the key in the object from arr1
matches with an id
within the nested config
and further within the questions
array, then replace that key (in the arr1
object) with the value of the title
property found next to that id
.
Let's see an example. The key isWorking
from arr1
is the same as the id
value in arr2.config[0].questions[0]
, so change the isWorking
key to the value found in
arr2.config[0].questions[0].custom.title
.
var arr1= [
{"jobs": "Marketing","isWorking": yes,"country": "MY"},
{"country": "IN","members": 4}
]
var arr2=
{
"id":1,
"name":"xxx",
"config":[
{
"questions":[
{
"id":"isWorking",
"custom":{
"title":"Are you working?"
}
},
{
"id":"jobs",
"custom":{
"title":"Please specify job(s)"
}
}
]
},
{
"questions":[
{
"id":"country",
"custom":{
"title":"which Country?"
}
},
{
"id":"members",
"type":"choices",
"custom":{
"title":"How many members?"
}
}
]
}
]
}
Expected output:
[
{"Please specify job(s)": "Marketing","Are you working": yes,"which Country": "MY"},
{"which Country": "IN","How many members": 4}
]
I attempted:
var result = arr1.map(e => ({
arr2.config.find(i => {
i.questions.find( q => {
q.id === Object.key(e) ? Object.key(e) === q.custom.title : q.id
}
})
}))