如何将地图结果合并为一个数组对象?

huangapple go评论73阅读模式
英文:

How to merge map result in to one array objects?

问题

重构后的对象结果处理函数在同一时间正常工作,但映射结果未以一个数组组的形式显示。

{
  "ageRange": [
    {
      "question": "ageRange",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    },
    {
      "question": "picAgeRange",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ],
  "goal": [
    {
      "question": "goal",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ],
  "hairColor": [
    {
      "question": "hairColor",
      "lastUpdated": "2018-07-09T18:13:49.567",
      "info": null,
      "type": "SELECT"
    },
    {
      "question": "picHairColor",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ]
}

现有结果是:

[
  {
    "ageRange": [
      {
        "question": "ageRange",
        "lastUpdated": "2018-07-09T18:13:42.541",
        "info": null,
        "type": "SELECT"
      },
      {
        "question": "picAgeRange",
        "lastUpdated": "2018-07-09T18:13:42.541",
        "info": null,
        "type": "SELECT"
      }
    ]
  },
  {
    "goal": [
      {
        "question": "goal",
        "lastUpdated": "2018-07-09T18:13:42.541",
        "info": null,
        "type": "SELECT"
      }
    ]
  },
  {
    "hairColor": [
      {
        "question": "hairColor",
        "lastUpdated": "2018-07-09T18:13:49.567",
        "info": null,
        "type": "SELECT"
      },
      {
        "question": "picHairColor",
        "lastUpdated": "2018-07-09T18:13:42.541",
        "info": null,
        "type": "SELECT"
      }
    ]
  }
]

期望的结果是:

{
  "ageRange": [
    {
      "question": "ageRange",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    },
    {
      "question": "picAgeRange",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ],
  "goal": [
    {
      "question": "goal",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ],
  "hairColor": [
    {
      "question": "hairColor",
      "lastUpdated": "2018-07-09T18:13:49.567",
      "info": null,
      "type": "SELECT"
    },
    {
      "question": "picHairColor",
      "lastUpdated": "2018-07-09T18:13:42.541",
      "info": null,
      "type": "SELECT"
    }
  ]
}
英文:

The object result restructuring function works fine at the same time the map result not coming in one array group.

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

console.clear();
const filterList = {
  ageRange: [&quot;ageRange&quot;, &quot;picAgeRange&quot;],
  goal: [&quot;goal&quot;, &quot;motivation&quot;],
  hairColor: [&quot;hairColor&quot;, &quot;picHairColor&quot;]
};

const questionList = [{
    question: &quot;ageRange&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;18-30&quot;,
        selected: true
      },
      {
        value: &quot;31-35&quot;,
        selected: false
      },
      {
        value: &quot;36-45&quot;,
        selected: false
      },
      {
        value: &quot;46-55&quot;,
        selected: false
      },
      {
        value: &quot;55+&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;picAgeRange&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;country 6&quot;,
        selected: true
      },
      {
        value: &quot;smart casual 4&quot;,
        selected: false
      },
      {
        value: &quot;dark denim 6&quot;,
        selected: false
      },
      {
        value: &quot;sporty 6&quot;,
        selected: false
      },
      {
        value: &quot;adventure 5&quot;,
        selected: false
      },
      {
        value: &quot;prints 5&quot;,
        selected: false
      },
      {
        value: &quot;excentric 5&quot;,
        selected: false
      },
      {
        value: &quot;dont like any private style&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;goal&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;save time&quot;,
        selected: true
      },
      {
        value: &quot;personal advice&quot;,
        selected: false
      },
      {
        value: &quot;inspiration&quot;,
        selected: false
      },
      {
        value: &quot;testing the service&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;hairColor&quot;,
    lastUpdated: &quot;2018-07-09T18:13:49.567&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;blond&quot;,
        selected: true
      },
      {
        value: &quot;brown&quot;,
        selected: false
      },
      {
        value: &quot;black&quot;,
        selected: false
      },
      {
        value: &quot;red&quot;,
        selected: false
      },
      {
        value: &quot;grey&quot;,
        selected: false
      },
      {
        value: &quot;noHair&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;picHairColor&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;suit 3&quot;,
        selected: true
      },
      {
        value: &quot;business casual&quot;,
        selected: false
      },
      {
        value: &quot;casual&quot;,
        selected: false
      },
      {
        value: &quot;have to wear uniform&quot;,
        selected: false
      },
      {
        value: &quot;classic&quot;,
        selected: false
      },
      {
        value: &quot;conservative&quot;,
        selected: false
      },
      {
        value: &quot;relaxed 2&quot;,
        selected: false
      },
      {
        value: &quot;dont like any work style&quot;,
        selected: false
      }
    ]
  }
];

const ofQuestionsList = Object.entries(filterList).map(
  ([questionKey, questionNames]) =&gt; {
    return {
      [questionKey]: questionList.filter((item) =&gt;
        questionNames.includes(item.question)
      )
    };
  }
);
console.log(ofQuestionsList);

<!-- end snippet -->

The existing result is

[
  {
    &quot;ageRange&quot;: [
      {
        &quot;question&quot;: &quot;ageRange&quot;,
        &quot;lastUpdated&quot;: &quot;2018-07-09T18:13:42.541&quot;,
        &quot;info&quot;: null,
        &quot;type&quot;: &quot;SELECT&quot;
      },
      {
        &quot;question&quot;: &quot;picAgeRange&quot;,
        &quot;lastUpdated&quot;: &quot;2018-07-09T18:13:42.541&quot;,
        &quot;info&quot;: null,
        &quot;type&quot;: &quot;SELECT&quot;
      }
    ]
  },
  {
    &quot;goal&quot;: [
      {
        &quot;question&quot;: &quot;goal&quot;,
        &quot;lastUpdated&quot;: &quot;2018-07-09T18:13:42.541&quot;,
        &quot;info&quot;: null,
        &quot;type&quot;: &quot;SELECT&quot;
      }
    ]
  },
  {
    &quot;hairColor&quot;: [
      {
        &quot;question&quot;: &quot;hairColor&quot;,
        &quot;lastUpdated&quot;: &quot;2018-07-09T18:13:49.567&quot;,
        &quot;info&quot;: null,
        &quot;type&quot;: &quot;SELECT&quot;
      },
      {
        &quot;question&quot;: &quot;picHairColor&quot;,
        &quot;lastUpdated&quot;: &quot;2018-07-09T18:13:42.541&quot;,
        &quot;info&quot;: null,
        &quot;type&quot;: &quot;SELECT&quot;
      }
    ]
  }
]

The expecting result is

{
  &quot;ageRange&quot;:[
    {
      &quot;question&quot;:&quot;ageRange&quot;,
      &quot;lastUpdated&quot;:&quot;2018-07-09T18:13:42.541&quot;,
      &quot;info&quot;:null,
      &quot;type&quot;:&quot;SELECT&quot;
    },
    {
      &quot;question&quot;:&quot;picAgeRange&quot;,
      &quot;lastUpdated&quot;:&quot;2018-07-09T18:13:42.541&quot;,
      &quot;info&quot;:null,
      &quot;type&quot;:&quot;SELECT&quot;
    }
  ],
  &quot;goal&quot;:[
    {
      &quot;question&quot;:&quot;goal&quot;,
      &quot;lastUpdated&quot;:&quot;2018-07-09T18:13:42.541&quot;,
      &quot;info&quot;:null,
      &quot;type&quot;:&quot;SELECT&quot;
    }
  ],
  &quot;hairColor&quot;:[
    {
      &quot;question&quot;:&quot;hairColor&quot;,
      &quot;lastUpdated&quot;:&quot;2018-07-09T18:13:49.567&quot;,
      &quot;info&quot;:null,
      &quot;type&quot;:&quot;SELECT&quot;
    },
    {
      &quot;question&quot;:&quot;picHairColor&quot;,
      &quot;lastUpdated&quot;:&quot;2018-07-09T18:13:42.541&quot;,
      &quot;info&quot;:null,
      &quot;type&quot;:&quot;SELECT&quot;
    }
  ]
}

答案1

得分: 3

Array.prototype.map() 返回一个数组,对于您的情况,如果我理解正确,您希望将元素分组并将它们作为一个结果对象返回。

这似乎可以通过使用 Array.prototype.reduce() 来实现,所以您可以这样做:

const ofQuestionsList = Object.entries(filterList).reduce(
  (acc, [questionKey, questionNames]) => {
    return {
      ...acc,
      [questionKey]: questionList.filter((item) =>
        questionNames.includes(item.question)
      )
    };
  }, {});

这个 reduce 接受两个参数,一个回调函数,它接收 accumulator(累加器)和正在处理的数组中的 current element(当前元素)。并且初始化累加器为一个空对象 {}

然后,在每次迭代中,我们都返回一个合并了迭代器的先前值和当前值的对象。

英文:

Array.prototype.map() returns an array, for your case if I understood correctly you want to group your elements and return them in one results object.

This seems like something that can be achieved with Array.prototype.reduce(), so you would have:

const ofQuestionsList = Object.entries(filterList).reduce(
  (acc, [questionKey, questionNames]) =&gt; {
    return {
      ...acc,
      [questionKey]: questionList.filter((item) =&gt;
        questionNames.includes(item.question)
      )
    };
  }, {});

This reduce takes 2 parameters, a callback taking the accumulator and your current element that being processed in your array. And the accumulator's initialization with an empty object {}.

Then in each iteration we return a merged object with the previous values of the iterator, and the current one

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

console.clear();
const filterList = {
  ageRange: [&quot;ageRange&quot;, &quot;picAgeRange&quot;],
  goal: [&quot;goal&quot;, &quot;motivation&quot;],
  hairColor: [&quot;hairColor&quot;, &quot;picHairColor&quot;]
};

const questionList = [{
    question: &quot;ageRange&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;18-30&quot;,
        selected: true
      },
      {
        value: &quot;31-35&quot;,
        selected: false
      },
      {
        value: &quot;36-45&quot;,
        selected: false
      },
      {
        value: &quot;46-55&quot;,
        selected: false
      },
      {
        value: &quot;55+&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;picAgeRange&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;country 6&quot;,
        selected: true
      },
      {
        value: &quot;smart casual 4&quot;,
        selected: false
      },
      {
        value: &quot;dark denim 6&quot;,
        selected: false
      },
      {
        value: &quot;sporty 6&quot;,
        selected: false
      },
      {
        value: &quot;adventure 5&quot;,
        selected: false
      },
      {
        value: &quot;prints 5&quot;,
        selected: false
      },
      {
        value: &quot;excentric 5&quot;,
        selected: false
      },
      {
        value: &quot;dont like any private style&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;goal&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;save time&quot;,
        selected: true
      },
      {
        value: &quot;personal advice&quot;,
        selected: false
      },
      {
        value: &quot;inspiration&quot;,
        selected: false
      },
      {
        value: &quot;testing the service&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;hairColor&quot;,
    lastUpdated: &quot;2018-07-09T18:13:49.567&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;blond&quot;,
        selected: true
      },
      {
        value: &quot;brown&quot;,
        selected: false
      },
      {
        value: &quot;black&quot;,
        selected: false
      },
      {
        value: &quot;red&quot;,
        selected: false
      },
      {
        value: &quot;grey&quot;,
        selected: false
      },
      {
        value: &quot;noHair&quot;,
        selected: false
      }
    ]
  },
  {
    question: &quot;picHairColor&quot;,
    lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
    info: null,
    type: &quot;SELECT&quot;,
    answers: [{
        value: &quot;suit 3&quot;,
        selected: true
      },
      {
        value: &quot;business casual&quot;,
        selected: false
      },
      {
        value: &quot;casual&quot;,
        selected: false
      },
      {
        value: &quot;have to wear uniform&quot;,
        selected: false
      },
      {
        value: &quot;classic&quot;,
        selected: false
      },
      {
        value: &quot;conservative&quot;,
        selected: false
      },
      {
        value: &quot;relaxed 2&quot;,
        selected: false
      },
      {
        value: &quot;dont like any work style&quot;,
        selected: false
      }
    ]
  }
];

const ofQuestionsList = Object.entries(filterList).reduce(
  (acc, [questionKey, questionNames]) =&gt; {
    return {
      ...acc,
      [questionKey]: questionList.filter((item) =&gt;
        questionNames.includes(item.question)
      )
    };
  }, {});

console.log(ofQuestionsList);

<!-- end snippet -->

答案2

得分: 0

管理结果

console.clear();
const result = [{
    ageRange: [{
        question: "ageRange",
        lastUpdated: "2018-07-09T18:13:42.541",
        info: null,
        type: "SELECT"
      },
      {
        question: "picAgeRange",
        lastUpdated: "2018-07-09T18:13:42.541",
        info: null,
        type: "SELECT"
      }
    ]
  },
  {
    goal: [{
      question: "goal",
      lastUpdated: "2018-07-09T18:13:42.541",
      info: null,
      type: "SELECT"
    }]
  },
  {
    hairColor: [{
        question: "hairColor",
        lastUpdated: "2018-07-09T18:13:49.567",
        info: null,
        type: "SELECT"
      },
      {
        question: "picHairColor",
        lastUpdated: "2018-07-09T18:13:42.541",
        info: null,
        type: "SELECT"
      }
    ]
  }
];

var newObj = result.reduce((a, b) => Object.assign(a, b), {})

console.log(newObj)
英文:

Manage with result

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

console.clear();
const result = [{
    ageRange: [{
        question: &quot;ageRange&quot;,
        lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
        info: null,
        type: &quot;SELECT&quot;
      },
      {
        question: &quot;picAgeRange&quot;,
        lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
        info: null,
        type: &quot;SELECT&quot;
      }
    ]
  },
  {
    goal: [{
      question: &quot;goal&quot;,
      lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
      info: null,
      type: &quot;SELECT&quot;
    }]
  },
  {
    hairColor: [{
        question: &quot;hairColor&quot;,
        lastUpdated: &quot;2018-07-09T18:13:49.567&quot;,
        info: null,
        type: &quot;SELECT&quot;
      },
      {
        question: &quot;picHairColor&quot;,
        lastUpdated: &quot;2018-07-09T18:13:42.541&quot;,
        info: null,
        type: &quot;SELECT&quot;
      }
    ]
  }
];

var newObj = result.reduce((a, b) =&gt; Object.assign(a, b), {})

console.log(newObj)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 09:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定