【发布时间】:2022-12-01 15:00:42
【问题描述】:
I have array object arr1 and object with nested array object arr2
if the object of arr1 key name matches with arr2 of key value then change the arr1 key and return the
object array using javascript.
from below arr1 and arr2
if key of arr1 matches with id value in questions in arr2, then change the arr1 key with title
and return array of object using javascript.
for example key name `isWorking` from arr1 is same as arr2.config.questions.id value
then change arr1 key to arr2.config.questions.title value
tried
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
}
})
}))
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?"
}
}
]
}
]
}
ExpectedOutput
[
{"Please specify job(s)": "Marketing","Are you working": yes,"which Country": "MY"},
{"which Country": "IN","How many members": 4}
]
【问题讨论】:
标签: javascript arrays typescript loops object