【问题标题】:How to change the object key from nested array object and return array object in javascriptHow to change the object key from nested array object and return array object in javascript
【发布时间】: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


    【解决方案1】:

    In your code the find callbacks do not return anything. When you have a code block (with braces) you need a return statement. Also, the outer object literal which you have as the return value of the map callback cannot have just the find call in it. It should have the proper object literal syntax, like with spread syntax. Moreover, the find method can only return an existing object, not a new, modified one.

    I will assume here that the matching strings for the first object have to be found in thefirstquestions array, and for the second object in the second questions array.

    I also propose to rename arr2, because it isn't an array. It is a plain object, with a property that is an array (config).

    Here is how you could do it with Object.fromEntries and Object.entries:

    const arr1 = [{"jobs": "Marketing","isWorking": "yes","country": "MY"}, {"country": "IN","members": 4}];
    
    const obj = {"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?"}}]}]}
    
    const result = arr1.map((e, i) => Object.fromEntries(
        Object.entries(e).map(([key, value]) =>
            [obj.config[i].questions.find(({id}) =>
                key === id
             )?.custom?.title ?? key, value]
        )
    ))
    
    console.log(result);

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-12-01
      • 2022-12-02
      • 2012-05-14
      • 2022-12-06
      • 2022-12-01
      • 1970-01-01
      • 2022-12-02
      • 2022-12-28
      相关资源
      最近更新 更多