首页 > 解决方案 > 如何构造一个包含对象数组的输出?

问题描述

假设我有这样的样本数据,

const questions_chklist = 
  { credit_broker_checklist: 
    [ { id: '1', title: 'Are any identified risks to the customer given equal prominence?', question_id: 'wmBHr' } 
    , { id: '2', title: 'Content has been approved by an authorised individual',            question_id: 'sPYvu' } 
    , { id: '3', title: 'Does it clearly name your company or the ?',                       question_id: '64bBL' } 
    ] 
  , test_checklist_checklist: 
    [ { id: '1', title: 'new questionn 1',     question_id: '0VsnL' } 
    , { id: '2', title: 'new question 02',     question_id: 'c9jrW' } 
    , { id: '3', title: 'New question 03',     question_id: 'fbJON' } 
    , { id: '4', title: 'new question 412234', question_id: 'AbcDE' } 
    ] 
  , new_list_checklist: [ ] 
  } 

现在我正在尝试创建一个这样的对象,

{ credit_broker_checklist: 
  [ { answer: '', comments: '', question_id: 'wmBHr' } 
  , { answer: '', comments: '', question_id: 'sPYvu' } 
  , { answer: '', comments: '', question_id: '64bBL' } 
  ] 
, test_checklist_checklist: 
  [ { answer: '', comments: '', question_id: '0VsnL' } 
  , { answer: '', comments: '', question_id: 'c9jrW' } 
  , { answer: '', comments: '', question_id: 'fbJON' } 
  , { answer: '', comments: '', question_id: 'AbcDE' } 
  ] 
, new_list_checklist: [ ] 
} 

目前,我不知道如何生成一个对象,所以我是这样生成的,

const questions_chklist = 
  { credit_broker_checklist: 
    [ { id: '1', title: 'Are any identified risks to the customer given equal prominence?', question_id: 'wmBHr' } 
    , { id: '2', title: 'Content has been approved by an authorised individual',            question_id: 'sPYvu' } 
    , { id: '3', title: 'Does it clearly name your company or the ?',                       question_id: '64bBL' } 
    ] 
  , test_checklist_checklist: 
    [ { id: '1', title: 'new questionn 1',     question_id: '0VsnL' } 
    , { id: '2', title: 'new question 02',     question_id: 'c9jrW' } 
    , { id: '3', title: 'New question 03',     question_id: 'fbJON' } 
    , { id: '4', title: 'new question 412234', question_id: 'AbcDE' } 
    ] 
  , new_list_checklist: [ ] 
  } 

let jsonObj = []
for (var key in questions_chklist)
  {
  if (questions_chklist.hasOwnProperty(key)) 
    {
    let option_count = questions_chklist[key].length;
    for (let i = 0; i < option_count; i++)
      {
      let item            = {} 
      item ["answer"]     = "";
      item ["comments"]   = "";
      item["question_id"] = questions_chklist[key][i].question_id
      jsonObj.push(item);
      }
    }
  }

console.log( jsonObj )
.as-console-wrapper { max-height: 100% !important; top: 0 }

它只是创建一个由几个对象组成的数组,

[
  {
    answer: '',
    comments: '',
    question_id: 'wmBHr'
  },
  {
    answer: '',
    comments: '',
    question_id: 'sPYvu'
  },
...
....

标签: javascript

解决方案


如果您希望 jsonObj 输出为对象,请不要将其初始化为数组。您所做的是遍历每个属性的值并将项目推送到 jsonObj 数组中。

如果我正确理解了您的问题,则可以对您的代码进行一些调整以输出您需要的内容。

let questions_chklist = {
    "credit_broker_checklist": [
        {
            "id": "1",
            "title": "Are any identified risks to the customer given equal prominence?",
            "question_id": "wmBHr"
        },
        {
            "id": "2",
            "title": "Content has been approved by an authorised individual",
            "question_id": "sPYvu"
        },
        {
            "id": "3",
            "title": "Does it clearly name your company or the ?",
            "question_id": "64bBL"
        }],
    "test_checklist_checklist": [
        {
            "id": "1",
            "title": "new questionn 1",
            "question_id": "0VsnL"
        },
        {
            "id": "2",
            "title": "new question 02",
            "question_id": "c9jrW"
        },
        {
            "id": "3",
            "title": "New question 03",
            "question_id": "fbJON"
        },
        {
            "id": "4",
            "title": "new question 412234",
            "question_id": "AbcDE"
        }
    ],
    "new_list_checklist": []
}

let jsonObj = {}
for (var key in questions_chklist) {
    if (questions_chklist.hasOwnProperty(key)) {
       jsonObj[key] = [];
       let option_count = questions_chklist[key].length;
       for (let i = 0; i < option_count; i++) {
          let item = {} 
          item ["answer"] = "";
          item ["comments"] = "";
          item["question_id"] = questions_chklist[key][i].question_id
          jsonObj[key].push(item);
       }               
   }
}
console.log(jsonObj)


推荐阅读