Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript object array merge

var arr1 = [{name: "lang", value: "English"},{name: "age", value: "18"}];
var arr2 = [{name : "childs", value: '5'}, {name: "lang", value: "German"}];

Array.prototype.push.apply(arr1,arr2); 

console.log(arr1);  // final merged result will be in arr1
Comment

concat array of objects javascript

const arr1 = [
    { name: "lang", value: "English" },
    { name: "age", value: "18" }
]
const arr2 = [
    { name: "childs", value: '2' }, 
    { name: "lang", value: "German" }
]
const arr3 = [
    { name: "lang", value: "German" },
    { name: "age", value: "28" },
    { name: "childs", value: '5' }
]

// Convert to key value dictionary or object
const convertToKeyValueDict = arrayObj => {
    const val = {}
    arrayObj.forEach(ob => {
        val[ob.name] = ob.value
    })
    return val
}

// update or merge array
const updateOrMerge = (a1, a2) => {
    const ob1 = convertToKeyValueDict(a1)
    const ob2 = convertToKeyValueDict(a2)
    // Note: Spread operator with objects used here
    const merged_obj = {...ob1, ...ob2}
    const val = Object.entries(merged_obj)
    return val.map(obj => ({ name: obj[0], value: obj[1] }))
}

const v1 = updateOrMerge(arr1, arr2)
const v2 = updateOrMerge(v1, arr3)
console.log(`Merged array1 and array2: ${JSON.stringify(v1)} 

`)
console.log(`Merged above response and array3: ${JSON.stringify(v2)} 

`)
Comment

merge array of objects javascript

Object.keys(arrayObject).map(key=>arrayObject[key]).reduce((old,item)=>(
          {...old,...item}
),{})
Comment

how to merge two object arrays in javascript

var objArr1 = [{id: "1", fruit: "apple"},{id: "2", fruit: "banana"}];
var objArr2 = [{id : "3", fruit: 'mango'}, {id: "4", fruit: "pear"}];

Array.prototype.push.apply(ObjArr1,ObjArr2); 

console.log(ObjArr1);  // final merged result will be in ObjArr1
// ObjArr1 => [{id: "1", fruit: "apple"},{id: "2", fruit: "banana"},{id : "3", fruit: 'mango'}, {id: "4", fruit: "pear"}]
Comment

join array of objects javascript

Object.values(objectsLIst).reduce((old,item)=>(
    {...old,...item}
),{})
Comment

Merge two array of Object in JavaScript

var arr1 = [{name: "lang", value: "English"},{name: "age", value: "18"}];
var arr2 = [{name : "childs", value: '5'}, {name: "lang", value: "German"}];

Array.prototype.push.apply(arr1,arr2); 

console.log(arr1);  // final merged result will be in arr1


/*
[{"name":"lang","value":"English"},
{"name":"age","value":"18"},
{"name":"childs","value":"5"},
{"name":"lang","value":"German"}]
*/
Comment

How to join two arrays of objects into one with JavaScript

const a = [{id: 1}, {id: 2}];
const b = [{id: 3}, {id: 4}];
// expected result: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
const result = a.concat(b);
console.log(result);
// outputs: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
Comment

Merge array of objects into one object

const arrObj = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}];

console.log(arrObj.reduce(function(result, current) {
  return Object.assign(result, current);
}, {}));

// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce(((r, c) => Object.assign(r, c)), {}));

// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery body remove class 
Javascript :: classiceditor is not defined using npm 
Javascript :: javascript create element input type text 
Javascript :: get top window url from iframe 
Javascript :: js object loop 
Javascript :: async import javascript 
Javascript :: reactjs cut part of string 
Javascript :: Link vs NavLink in react-router-dom 
Javascript :: update object in array if id exists else append javascript 
Javascript :: dm command discord.js 
Javascript :: get the text of a tag 
Javascript :: js array includes 
Javascript :: javascript remove array element 
Javascript :: generate express js project 
Javascript :: file upload javascript 
Javascript :: Immediately-Invoked Function javascript 
Javascript :: Lazy Loading Routes vue 
Javascript :: angularjs find and update object in array 
Javascript :: mongodb data types 
Javascript :: parse time in javascript 
Javascript :: file name in react input 
Javascript :: copy to clipboard jquery example 
Javascript :: express error middleware 
Javascript :: stylesheet create 
Javascript :: react click outside 
Javascript :: how to call function from parent component in child component vue 
Javascript :: create a loop that runs through each item in an array 
Javascript :: how to appendChild in the begin of the div javascript 
Javascript :: axios patch 
Javascript :: javascript remove element from object 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =