【问题标题】:javascript extract id's from two levels of arrays for key value pairs in new objectjavascript从新对象中键值对的两级数组中提取id
【发布时间】:2018-06-27 20:58:38
【问题描述】:

我正在返回一个有效负载,我需要从有效负载内的两个单独级别中提取 id,并将其作为键值对的单个对象返回。我试图做某种组合或ForEach()reduce(),但我似乎找不到正确的方法。这是数据的样子。

{
 orderId:999,
 menuId: 123456,
  questions:[{
    questionId: 123,
    depth: 1,
        answers: [
            { answerId: 999, text: "foo1" },
            { answerId: 888, text: "foo2" }]
  },
  {
    questionId: 654,
    depth: 1,
    answers: [{ answerId: 777, text: "bar" }]
  }]
}

我试图得到的结果

{"q_123": ["999", "888"], "q_654": "777"}

【问题讨论】:

    标签: javascript arrays javascript-objects


    【解决方案1】:

    reduce 的以下方法可以完成这项工作:

    const data = {
      orderId: 999,
      menuId: 123456,
      questions: [{
          questionId: 123,
          depth: 1,
          answers: [{
              answerId: 999, 
              text: "foo1"
            }, {
              answerId: 888,
              text: "foo2"
            }
          ]
        }, {
          questionId: 654,
          depth: 1,
          answers: [{
            answerId: 777,
            text: "bar"
          }]
        }
      ]};
    
    const result = data.questions.reduce((all, {
      questionId: id,
      answers
    }) => {
    
      all[`q_${id}`] = answers.map(a => a.answerId);
    
      return all;
    }, {});
    console.log(result);

    【讨论】:

    • aww,我没有正确使用 reduce。谢谢,效果很好!
    【解决方案2】:

    您可以使用reduce 来实现:

    const data = {
      orderId: 999, menuId: 123456, questions: [
        {
          questionId: 123,
          depth: 1,
          answers: [
            { answerId: 999, text: "foo1"},
            { answerId: 888, text: "foo2"}
          ]
        },
        {
          questionId: 654,
          depth: 1,
          answers: [ { answerId: 777, text: "bar" }]
        }
      ]
    }
    
    const result = data.questions.reduce((acc, {questionId, answers}) => {
      return Object.assign({
        [`q_${questionId}`]: answers.map(
          ({answerId}) => answerId
        )
      }, acc)
    }, {});
    
    console.log(result)

    【讨论】:

      【解决方案3】:

      这是简单的JSON parsing

      var input = {
       orderId:999,
       menuId: 123456,
        questions:[{
          questionId: 123,
          depth: 1,
              answers: [
                  { answerId: 999, text: "foo1" },
                  { answerId: 888, text: "foo2" }]
        },
        {
          questionId: 654,
          depth: 1,
          answers: [{ answerId: 777, text: "bar" }]
        }]
      };
      
      var length = input.questions.length;
      var questionsobj = input.questions;
      var resultJsonData = {};
      
      for(var i=0; i<length; i++) {   
         var question_id = questionsobj[i].questionId;
         var answersobj = questionsobj[i].answers;
         var len = answersobj.length;
         var arrayObject = [];
         
         for(var k=0; k<len; k++) {
            arrayObject[k] = answersobj[k].answerId;
         }
         resultJsonData["q_" + question_id] = arrayObject;
      }
      
      console.log(JSON.stringify(resultJsonData));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-18
        • 2019-05-01
        • 1970-01-01
        • 2023-03-08
        • 2022-06-16
        相关资源
        最近更新 更多