Merge Array of Objects by Same Key-Value

merge javascript objects in array with same key
merge two array of objects based on a key
javascript merge two arrays of objects without duplicates
javascript merge key value pairs
javascript merge array of objects by key lodash
merge two array of objects based on a key lodash
merge two arrays as key value pairs javascript
union array of objects javascript

I have an array of objects like so:

[
  {
    "name": "Blah",
    "description": "This is a description"
  },
  {
    "name": "Foo",
    "description": "This is a description"
  },
  {
    "name": "Bar",
    "description": "This is a description"
  },
  {
    "name": "Blah",
    "description": "This is a description"
  },
  {
    "name": "Test",
    "description": "This is a description"
  }
]

I'd like to run some kind of function to combine the objects with the same name in a description1 + "\n" + description2 pattern, so the end result is:

[
  {
    "name": "Foo",
    "description": "This is a description"
  },
  {
    "name": "Bar",
    "description": "This is a description"
  },
  {
    "name": "Blah",
    "description": "This is a description\nThis is a description"
  },
  {
    "name": "Test",
    "description": "This is a description"
  }
]

The order of the array doesn't need to be preserved. Any help would be appreciated. Thanks!

You can simply use Array.reduce() to create a map, grouped by name and Object.values() on the map will give your the desired result:

let arr = [ { "name": "Blah", "description": "This is a description" }, { "name": "Foo", "description": "This is a description" }, { "name": "Bar", "description": "This is a description" }, { "name": "Blah", "description": "This is a description" }, { "name": "Test", "description": "This is a description" } ];
let result = Object.values(arr.reduce((a, {name,description})=>{
  if(a[name])
    a[name].description += "\n" + description;
   else
    a[name] = Object.assign({},{name,description});
   return a;
},{}));
console.log(result);

Union of objects in javaScript based on unique value, Merging of two or more objects using object destructuring​​ Using es6 object destructuring, it is very easy to merge two objects. What if both the object has same key, it simply merge the last objects value and have only one key value. Now we need to merge the two array of objects into a single array by using id property because id is the same in both array objects. Note: Both arrays should be the same length to get a correct answer. First way. Here we are the using map method and Object.assign method to merge the array of objects by using id.

You could use a lookup object to find duplicates:

 const result = [], hash = {};
 for(const {name, description} of input) {
    if(hash[name]) {
      hash[name].description += "\n" + description;
    } else {
      result.push(hash[name] = { name, description });
   }
 }

How to merge properties of two JavaScript objects dynamically , How to store a key=> value array in JavaScript ? Create a Output: Example 2: Suppose the objects have the same keys. How to merge two PHP objects? You can probably do this as a one liner, but for readability and efficiency it might be nicer to make a lookup object based on the value you want from one of the arrays and then map the other other array using the lookup to join the other value you want.

You can use reduce and findIndex to check if the new array have any object where the name matches. If it so then update the description

let orgArray = [{
    "name": "Blah",
    "description": "This is a description"
  },
  {
    "name": "Foo",
    "description": "This is a description"
  },
  {
    "name": "Bar",
    "description": "This is a description"
  },
  {
    "name": "Blah",
    "description": "This is a description"
  },
  {
    "name": "Test",
    "description": "This is a description"
  }
]

let newArray = orgArray.reduce(function(acc, curr) {
  let findIndexs = acc.findIndex(function(item) {
    return item.name === curr.name;

  })
  if (findIndexs === -1) {
    let obj = {
      name: curr.name,
      description: curr.description
    }
    acc.push(obj)
  } else {
    acc[findIndexs].description = acc[findIndexs].description + ' \n ' + curr.description
  }
  return acc;
}, []);
console.log(newArray)

JavaScript merge array of objects by key (es6), Suppose we have two array of objects with the same key. Example: //​incrementing start value start = start+1 } return merge; }. Share: facebook  Best How To : As per my comment, this solution depends on the structure changing very slightly, to be arrays of associative arrays, not arrays of objects.As you said the data is coming from (a) database(s), if you're using something like PDO, this should just mean a small change to set the correct fetch mode.

JavaScript: Merge duplicate objects in array of objects, You have array of objects where some of them are duplicates but with different array and check if we already have key-value pair inside Map and append to  Effectively merge arrays of objects into a single array by merging objects with the same key (property). Also use this module to merge objects with the same property value in one array. This is a micro module, which includes light and well-tested code.

JavaScript, When we have two separate array and we want to make key value pair from that two array, we can use array's reduce function like below: var columns = ["Date"  How to intersect arrays of objects based on object property value; How to joins and aggregates in Javascript arrays; How to load data from multiple JSON files; How to match the sub property in an objects array; How to merge object arrays based on join of a key property in each; How to parse a Json with array and objects and export the data into

How to merge content of two or more objects in JavaScript?, is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. I have an array with 45 object elements containig id, name, is_premium. From MySQL I receive them, ordered by is_premium desc, and some of them have is_premium = 0 at the end of list. How can I randomise elements with is_premium=1, keeping the is_premium=0 at the end of array?

Comments
  • What did you try?
  • Please read How do I ask a good question?
  • Searched and researched, check. No similar question existed. Summed up question with one middle sentence, plus two properly-formatted before and after examples. No spelling, grammar, or punctuation errors. Title sums up problem without too much detail. Introduced problem, check. Helped others reproduce, check. Added relevant tags, check. Proofread, check. Respond to answers, check. The only code missing was the solution that I didn't have. I'll keep trying to improve my question-asking form, thanks.
  • @xd1936 You got my and Robidu's responses because your question shows 0 effort, 0 research - it just asks for help. When you've done research and tried things, include them! You will get much better responses
  • Fantastic! Thanks for the response. Works great with EMCAScript 2015 and above because of Object.assign().
  • Interesting. I had no idea you could do a for...of in this capacity. Very clever! Thanks!