Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 合并来自同一数组的json数据_Javascript_Jquery_Arrays_Json - Fatal编程技术网

Javascript 合并来自同一数组的json数据

Javascript 合并来自同一数组的json数据,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我有一个json数组,如下所示: var arr1 = [ {id : 1, branchId : 1, branchDetail : "yes"}, {id : 1, branchId : 2, branchDetail : "no"}, {id : 2, branchId : 1, branchDetail : "yes"} ]; 如果数据具有相同的id,我希望合并这些数据,并创建另一个数据来处理合并。比如这个: var fi

我有一个json数组,如下所示:

    var arr1 = [
      {id : 1, branchId : 1, branchDetail : "yes"}, 
      {id : 1, branchId : 2, branchDetail : "no"}, 
      {id : 2, branchId : 1, branchDetail : "yes"}
    ];
如果数据具有相同的id,我希望合并这些数据,并创建另一个数据来处理合并。比如这个:

    var finalArr = [
        {id : 1, 
         branch : [
            {branchId:1, branchDetail:"yes" },
            {branchId : 2, branchDetail : "no"}
         ]
        }, 
        {id : 2, 
         branch : [
            {branchId : 1, branchDetail : "yes"}
         ]
        }
    ];
我认为$.extend()不是正确的方法,因为它只覆盖键中最后提供的值

在显示器上处理还是在数据显示之前处理更好


关于

您可以使用reduce、map和Object.key执行此操作:

var arr1 = [
   {id : 1, branchId : 1, branchDetail : "yes"}, 
   {id : 1, branchId : 2, branchDetail : "no"}, 
   {id : 2, branchId : 1, branchDetail : "yes"}
 ];


var object = arr1.reduce(function(memo, item){
  // initialise item
  (memo[item.id] = memo[item.id] || {id: item.id, branch: []})

  // add the branch to it
  .branch.push({
      branchId: item.branchId, branchDetail: item.branchDetail
  });

  return memo;
},{})

var finalArray = Object.keys(object)
  .map(function(k){
    return object[k]
  })

console.log(finalArray);

可以使用reduce、map和Object.keys执行此操作:

var arr1 = [
   {id : 1, branchId : 1, branchDetail : "yes"}, 
   {id : 1, branchId : 2, branchDetail : "no"}, 
   {id : 2, branchId : 1, branchDetail : "yes"}
 ];


var object = arr1.reduce(function(memo, item){
  // initialise item
  (memo[item.id] = memo[item.id] || {id: item.id, branch: []})

  // add the branch to it
  .branch.push({
      branchId: item.branchId, branchDetail: item.branchDetail
  });

  return memo;
},{})

var finalArray = Object.keys(object)
  .map(function(k){
    return object[k]
  })

console.log(finalArray);

尝试使用
Array.prototype.forEach()

var arr1=[{
id:1,
布朗希德:1,
branchDetail:“是的”
}, {
id:1,
布朗希德:2,
branchDetail:“否”
}, {
id:2,
布朗希德:1,
branchDetail:“是的”
}];
var finalArr=[{
id:1,
分行:[]
}, {
id:2,
分行:[]
}];
arr1.forEach(函数(a、b、数组){
[a]
.forEach(功能(val,键){
最终forEach(函数(obj,k){
if(obj.id==val.id){
分岔({
布兰奇:瓦尔·布兰奇,
branchDetail:val.branchDetail
})
}
})
})
});
$(“pre”).text(JSON.stringify(finalArr,null,2))

尝试使用
Array.prototype.forEach()

var arr1=[{
id:1,
布朗希德:1,
branchDetail:“是的”
}, {
id:1,
布朗希德:2,
branchDetail:“否”
}, {
id:2,
布朗希德:1,
branchDetail:“是的”
}];
var finalArr=[{
id:1,
分行:[]
}, {
id:2,
分行:[]
}];
arr1.forEach(函数(a、b、数组){
[a]
.forEach(功能(val,键){
最终forEach(函数(obj,k){
if(obj.id==val.id){
分岔({
布兰奇:瓦尔·布兰奇,
branchDetail:val.branchDetail
})
}
})
})
});
$(“pre”).text(JSON.stringify(finalArr,null,2))


所以,如果我除了id之外还有其他钥匙,我会在这个零件上逐个插入它们吗<代码>(memo[item.id]=memo[item.id]|{id:item.id,otherKey1:otherValue1,otherKey2:otherValue2,…,otherKeyN:otherValueN,branch:[]})
?或者还有其他方法?好的,通过添加其他键,我现在使用$.extend()<代码>$.extend(备注[item.id],item)
所以,如果我除了id之外还有其他键,我会在这个部件上逐个插入它们吗<代码>(memo[item.id]=memo[item.id]|{id:item.id,otherKey1:otherValue1,otherKey2:otherValue2,…,otherKeyN:otherValueN,branch:[]})?或者还有其他方法?好的,通过添加其他键,我现在使用$.extend()<代码>$.extend(备注[item.id],item)