合并数组内的JSON对象

时间:2019-04-08 09:16:25

标签: javascript arrays json

我有一个如下的JSON数组

[
{"Name" : "Arrow",
"Year" : "2001"
},

{"Name" : "Arrow",
"Type" : "Action-Drama"
},
{ "Name" : "GOT",
"Type" : "Action-Drama"
}
]

并且我正在尝试将其转换为

[
  { 
    "Name" : "Arrow",
    "Year" : "2001",
    "Type" : "Action-Drama",
  },
  {
   "Name" : "GOT",
   "Type" : "Action-Drama"
  }
]

任何帮助,我们将不胜感激。

谢谢。

4 个答案:

答案 0 :(得分:2)

您可以使用Array#reduce方法。

let data = [{
    "Name": "Arrow",
    "Year": "2001"
  },

  {
    "Name": "Arrow",
    "Type": "Action-Drama"
  },
  {
    "Name": "GOT",
    "Type": "Action-Drama"
  }
]


let res = data.reduce((obj, { Name, ...rest }) => {
  // define property(key as Name) if not defined
  obj[Name] = obj[Name] || { Name };
  // copy rest properties to object
  Object.assign(obj[Name], rest);
  // return object reference
  return obj;
  // set initial value as epty object
}, {})

// or one liner solution
// let res = data.reduce((obj, { Name, ...rest }) => (obj[Name]={... obj[Name] || {Name}, ...rest },obj), {})



// now res holds an object where key id name
// get values as an array using Object.values method
console.log(Object.values(res))


使用传统的for循环,并带有一个附加对象,该对象使用Name作为键来保留每个元素的引用。

let data = [{
    "Name": "Arrow",
    "Year": "2001"
  },

  {
    "Name": "Arrow",
    "Type": "Action-Drama"
  },
  {
    "Name": "GOT",
    "Type": "Action-Drama"
  }
]

// initialize array for result
let res = [],
  // an object for array value reference by name
  ref = {};

// iterate over main array
for (let i = 0; i < data.length; i++) {
  // check property defined in refeence object(Name)
  // if not then define and pushj it to result array
  if (!(data[i].Name in ref)) res.push(ref[data[i].Name] = {});
  // copy propety to object
  Object.assign(ref[data[i].Name], data[i]);
}

console.log(Object.values(res))

答案 1 :(得分:1)

您可以使用reduce()findIndex()

let data = [
{"Name" : "Arrow",
"Year" : "2001"
},

{"Name" : "Arrow",
"Type" : "Action-Drama"
},
{ "Name" : "GOT",
"Type" : "Action-Drama"
}
]


let res = data.reduce((ac,a) => {
  let ind = ac.findIndex(x => x.Name === a.Name);
  ind === -1 ? ac.push({...a}) : ac[ind] = {...ac[ind],...a};
  return ac;


},[])
console.log(res)

答案 2 :(得分:0)

请尝试这个

var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};
var object2 = {
  banana: { price: 200 },
  durian: 100
};

// Merge object2 into object1, recursively
$.extend( true, object1, object2 );

console.log( JSON.stringify( object1 ) );

参考:

http://researchhubs.com/post/computing/javascript/merge-content-of-objects.html

答案 3 :(得分:0)

使用reduceObject.assign合并数组中的项目:

const data = [{
  "Name" : "Arrow",
  "Year" : "2001"
}, {
  "Name" : "Arrow",
  "Type" : "Action-Drama"
}, {
  "Name" : "GOT",
  "Type" : "Action-Drama"
}];



function mergeByProp (prop, xs) {
  return xs.reduce((acc, x) => {
    if (!acc[x[prop]]) {
      acc[x[prop]] = x;
    } else {
      acc[x[prop]] = Object.assign(acc[x[prop]], x);
    }
    return acc;
  }, {});
}

function objToArr (obj) {
  return Object.keys(obj).map(key => obj[key]);
}

console.log(objToArr(mergeByProp('Name', data)));

相关问题