Merge Array&对象数组

时间:2018-05-14 13:51:23

标签: javascript arrays

尝试将数组对象与数组合并,同时保持原始数组的顺序并在结尾添加任何新值。这可以使用ES6 / ES7语法单​​行完成吗?

const arr1 = [7, 1, 2, 5, 8];

const arr2 = [
    {id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
    {id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
    {id: 2, type: "Text", name: "Test Create Property"},
    {id: 10, type: "Label", name: "New Label"},
    {id: 1, type: "Text", name: "Test Text Property"},
    {id: 7, type: "Label", name: "This is a crazy new one"},
    {id: 9, type: "Text", name: "New Text"}
];

我尝试了以下操作,导致所有键被添加到具有所需顺序的数组中,并在末尾添加第二个数组作为新键:

const arr3 = [];
arr3 = arr1.map(x => ([...arr1, arr2]));

输出:

arr3 = [
    [7, 1, 2, 5, 8,
    [
      {id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
      {id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
      {id: 2, type: "Text", name: "Test Create Property"},
      {id: 10, type: "Label", name: "New Label"},
      {id: 1, type: "Text", name: "Test Text Property"},
      {id: 7, type: "Label", name: "This is a crazy new one"},
      {id: 9, type: "Text", name: "New Text"}
    ]
  ]
];

我也尝试过导致一系列谬误:

const arr3 = [];
arr3 = arr1.map(id => arr3.push(arr2.id === id));

输出:

arr3 = [ false, false, false, false, false ];

预期输出是合并的单个对象数组:

const arr3 = [
    {id: 7, type: "Label", name: "This is a crazy new one"},
    {id: 1, type: "Text", name: "Test Text Property"},
    {id: 2, type: "Text", name: "Test Create Property"},
    {id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
    {id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
    {id: 10, type: "Label", name: "New Label"},
    {id: 9, type: "Text", name: "New Text"}
];

2 个答案:

答案 0 :(得分:0)

您可以使用sort方法,并根据index中当前元素ID的arr1进行排序。

const arr1 = [7, 1, 2, 5, 8];
const arr2 = [{"id":8,"type":"MultiChoiceMultiAnswer","name":"Another Test Property"},{"id":5,"type":"MultiChoiceMultiAnswer","name":"test"},{"id":2,"type":"Text","name":"Test Create Property"},{"id":10,"type":"Label","name":"New Label"},{"id":1,"type":"Text","name":"Test Text Property"},{"id":7,"type":"Label","name":"This is a crazy new one"},{"id":9,"type":"Text","name":"New Text"}]

arr2.sort((a, b) => {
  const iA = arr1.indexOf(a.id);
  const iB = arr1.indexOf(b.id);
  return ((iB != -1) - (iA != - 1)) || iA - iB
})

console.log(arr2)

答案 1 :(得分:0)

试试这个



const arr1 = [7, 1, 2, 5, 8];

const arr2 = [{
    id: 8,
    type: "MultiChoiceMultiAnswer",
    name: "Another Test Property"
  },
  {
    id: 5,
    type: "MultiChoiceMultiAnswer",
    name: "test"
  },
  {
    id: 2,
    type: "Text",
    name: "Test Create Property"
  },
  {
    id: 10,
    type: "Label",
    name: "New Label"
  },
  {
    id: 1,
    type: "Text",
    name: "Test Text Property"
  },
  {
    id: 7,
    type: "Label",
    name: "This is a crazy new one"
  },
  {
    id: 9,
    type: "Text",
    name: "New Text"
  }
];

let output = [];

arr1.forEach(function(itm) {
  var indx = arr2.findIndex(function(itm2) {
    return itm == itm2.id;
  })
  if (indx != -1) {
    output.push(arr2[indx]);
    arr2.splice(indx, 1);
  }
});
arr2.forEach(itm => output.push(itm))
console.log(output)