How to merge values from multiple objects into an object of arrays?

javascript merge array of objects by key
merge two array of objects based on a key
javascript merge objects with same key
javascript merge key value pairs
merge objects javascript
merge multiple objects javascript
es6 merge array of objects
merge object into array of objects

I have a two JSON something like below:

var obj1 = {
    " name ":"rencho",
    " age ":23,
    " occupation ":"SE"

var obj2 = {
    " name ":"manu",
    " age ":23,
    " country ":"india"

I want the expected output:

var result = {
    "name":["rencho", "manu"],
    "age":[23, 23],
    "country":["-", "india"],
    "occupation": ["SE", "-"],

However, I tried using below the code snippet:

let arrGlobal = []


let mergedResult = arrGlobal.reduce(function(r, e) {
            return Object.keys(e).forEach(function(k) {
            if(!r[k]) r[k] = [].concat(e[k])
            else r[k] = r[k].concat(e[k])
            }), r
        }, {})


But that one doesn't print - in json object. I would appreciate any kind of help from your side.


First get a list of all keys (needed in advance to check whether you need to add - while iterating), then use reduce to iterate over each object and add its values to the accumulator:

var obj1 = {
    " name ":"rencho",
    " age ":23,
    " occupation ":"SE"

var obj2 = {
    " name ":"manu",
    " age ":23,
    " country ":"india"
const arr = [obj1, obj2];
const allKeys = arr.reduce((keys, obj) => {
  Object.keys(obj).forEach(key => keys.add(key))
  return keys;
}, new Set());
const merged = arr.reduce((merged, obj) => {
  allKeys.forEach((key) => {
    if (!merged[key]) merged[key] = [];
    merged[key].push(obj[key] || '-');
  return merged;
}, {});

How to merge values from multiple objects into an object of arrays , First get a list of all keys (needed in advance to check whether you need to add - while iterating), then use reduce to iterate over each object  So here's the quick rule. If you know you're dealing with arrays, use spread. But if you might be dealing with the possibility with a non-array, then use concat to merge an array 👍 Anyways I just want to point that out, so you can use the most appropriate method depending on the problem you're trying to solve 👍 # Merge Array with Push 🤔

A slightly different approach by using a single loop for the outer array of objects and which generates all needed keys on the fly.

var obj1 = { name: "rencho", age: 23, occupation: "SE" },
    obj2 = { name: "manu", age: 23, country: "india" },
    hash = new Set,
    result = {};

[obj1, obj2].forEach((o, length) => {
    Object.keys(o).forEach(k => hash.add(k));
    hash.forEach(k => {
        result[k] = result[k] || Array.from({ length }).fill('-');
        result[k].push(k in o ? o[k] : '-');

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

How to merge properties of two JavaScript objects dynamically , How to convert an Object {} to an Array [] of key-value pairs in JavaScript? Design a We can merge different objects into one using the spread (…) operator. Spread the love Related Posts Cloning Arrays in JavaScriptThere are a few ways to clone an array in JavaScript, Object.assign Object.assign allows us… How to Merge Arrays in JavaScriptOftentimes, we need to combine multiple arrays into one array. With multiple arrays, combining them… Append Item to a JavaScript ArrayAppending an item to an array in …

quick and dirty way:

function merge(a,b) {
  var c = b
  for (key in a){
    c[key] = [c[key], a[key]]
  console.log(c) //prints merged object

merge(obj1, obj2)

How to merge two objects in JavaScript, In this tutorial, we will see examples of. Merging of two or more objects; Merging of object into an Array; Merging two arrays; Union of objects  Creating a JavaScript Object from two arrays. This will return an array of single-key objects; it will not combine everything into one object. value} pairs

Union of objects in javaScript based on unique value, is for working with arrays and other iterable objects. while I tinker with Babel and React: merging multiple objects' properties into one object! The JavaScript. Wrap any objects you'd like merged into one with braces ( {} ): A new object is created containing all properties and values from the objects  merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join.

Merge Object Properties with the Spread Operator, Vanilla JavaScript doesn't offer any native method to merge objects together. (​Technically, you could use Object.assign(), but it has some We can do this with multiple objects to merge them together. you can use the arguments variable to access an array of arguments that were passed into your method  how to combine objects using powershell? Use this function if you need to combine two custom powershell objects that come from differents scripts /functions

Merging objects with vanilla JavaScript, valueN Optional: Arrays and/or values to concatenate into a new The concat method creates a new array consisting of the elements in the object on which objects): concat copies the values of strings and numbers into the  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.

  • Do your original object keys really have spaces around them?
  • Original doesn't have. However, it prints key something like below " name ". Is it going to affect?
  • That's not JSON. "JSON is a textual, language-indepedent data-exchange format, much like XML, CSV or YAML." - What is the difference between JSON and Object Literal Notation?
  • Why python, java and arrays are in tags?
  • This question has nothing to do with python, java, or JSON.