如何将具有相同键的两个对象(关联数组)合并到一个对象

时间:2017-12-02 19:48:18

标签: javascript arrays

我有两个对象:

var arr= [
  0: { selectedAns: "test answer 1", id: "28", status: "answered" }
]

var answerList = [
  0: { selectedAns: "test answer 2 ques 5", id: "27", status: "answered" },
  1: { selectedAns: "test answer 3 ques 6", id: "29", status: "answered"}
]

当我这样做时:

arr1.push(arr);
arr1.push(answerList);

我得到了这个结果:

var arr1=[0:{
    0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"}
}
1:{
    0:{selectedAns: "test answer 1", id: "28", status: "answered"}
    1:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"}
}]

但我想要这样的事情:

    var arr1=[
    0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"}

    1:{selectedAns: "test answer 1", id: "28", status: "answered"}

    2:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"}
]

我做错了什么?感谢。

注意:我在合并两个对象之前从对象中删除重复项,即使任何键在状态键中具有重复值。不过,我需要合并两个对象。根据我的要求,只有id键必须是唯一的,所以我需要一种方法来合并上面的两个对象。

2 个答案:

答案 0 :(得分:2)

您可以使用Array#concat,这需要分配新数组。



var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }],
    answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }];
    
arr = arr.concat(answerList);

console.log(arr);

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




或者使用Array#pushFunction#apply将数组作为参数。



var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }],
    answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }];
    
Array.prototype.push.apply(arr, answerList);

console.log(arr);

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




使用ES6,您可以使用spread syntax ...来推送值,该值将数组作为参数。



var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }],
    answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }];
    
arr.push(...answerList);

console.log(arr);

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




答案 1 :(得分:0)

使用javascript你可以使用for循环将answerList数组推送到arr1看看这个函数:

var arr1 = [];
var arr={0:{selectedAns: "test answer 1", id: "28", status: "answered"}};

var answerList={0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"},1:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"}};

arr1.push(arr);//push first array
pashingarray(answerList);

//function to push an array
function pashingarray(arrtobepush)
{
var lenghtar = 0;
for(var key in arrtobepush) {
    if(arrtobepush.hasOwnProperty(key)){
       lenghtar++;//get the lenght of the array
    }
}
for(var i = 0;i<lenghtar;i++)//loop the array to push the content
{
  var keytopush = i+1;
  var arraytopush = {[keytopush]:arrtobepush[i]};
  arr1.push(arraytopush);
}
}

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