Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

merge 2 array of object by key

let arr1 = [
    { id: "abdc4051", date: "2017-01-24" },
    { id: "abdc4052", date: "2017-01-22" }
];

let arr2 = [
    { id: "abdc4051", name: "ab" },
    { id: "abdc4052", name: "abc" }
];

let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));

console.log(arr3);
 Run code snippet
Comment

js combine 2 array to object key value

var columns = ["Date", "Number", "Size", "Location", "Age"];
var rows = ["2001", "5", "Big", "Sydney", "25"];
var result =  rows.reduce(function(result, field, index) {
  result[columns[index]] = field;
  return result;
}, {})

console.log(result);
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 :: axios get 
Javascript :: js remove last character 
Javascript :: Javascript Show HTML Elements 
Javascript :: open folder node js 
Javascript :: mapdispatchtoprops 
Javascript :: jquery chrome extension 
Javascript :: How to set the background image URL of an element using jQuery 
Javascript :: photo in React native 
Javascript :: element.classname javascript 
Javascript :: javascript href on load delay 
Javascript :: how to load link in new window using js 
Javascript :: check checkbox by jquery 
Javascript :: jquery get all value from class 
Javascript :: filter array of objects with array of objects 
Javascript :: debug.xcconfig: unable to open file react native 
Javascript :: toastr.success 
Javascript :: python append to json file 
Javascript :: filter json array by key in angular 9 
Javascript :: javascript get query params from url 
Javascript :: add image to ag-grid with react 
Javascript :: check date js 
Javascript :: js click change img 
Javascript :: in text includes in aray of objects 
Javascript :: mongoose save or update 
Javascript :: react 360 
Javascript :: javjquery is emptyobject 
Javascript :: javascript math random floor 
Javascript :: get the last array element javascript 
Javascript :: convert inches to feet javascript 
Javascript :: remove all elements of one array from another javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =