merge two JavaScript objects with priority [duplicate] - javascript

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 6 years ago.
I have two JavaScript objects like
let master = {A:0, B:2};
let slave = {B:1, C:1};
I need:
result == {A:0, B:2, C:1};
Does JS have a simple command to merge in that way?

Use Object.assign
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
let master = {A:0, B:2};
let slave = {B:1, C:1};
console.log(Object.assign(slave, master));

Related

Why cant we iterate over Object? [duplicate]

This question already has answers here:
Why are Objects not Iterable in JavaScript?
(7 answers)
Closed 2 months ago.
why Objects are not iterable? bcoz they are , we can iterate with for in loop and why to use 'iterable objects' and push Symbol.iterator in Object?!
You may use the Object.keys to iterate the object. The Object.keys returns an iterable array containing the keys of the object. See example:
var obj = { a:1, b:2, c:100, d:120};
var objItems = Object.keys(obj);
objItems.forEach( (item, i)=>{
console.log(obj[item]);
});

How to deconstruct arrays of arrays in Javascript [duplicate]

This question already has answers here:
JavaScript flattening an array of arrays of objects
(14 answers)
Closed 4 months ago.
I'm working with an API and after I try to clean up the data, I got an array of arrays of arrays:
arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]]
How can I clean this up to something like this:
arr = [{name: "john"},{name: "jack"},{name: "joe"},{name: "bob"}]
You can use Array.prototype.flat(), providing Infinity as the depth argument to flatten all sub-arrays recursively:
const arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]];
const flattened = arr.flat(Infinity);
console.log(flattened);
In this case calling arr.flat().flat() would do the trick.

Create a subset object, consisting of only some of the properties of an existing object [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
How to get a subset of a javascript object's properties
(36 answers)
Closed 7 years ago.
This is best explained by example. The following works in es6 to create an object consisting of some of the keys of an existing object:
var o = {a:1, b: 2, c: 3}
var {a, c} = o
var subsetObj = {a, c} // will be: {a:1, c:3}
There are two downsides here:
It took two statments, and two lines, to create the desired subset object
We had to pollute the local variable scope by creating the intermediary a and c variables, which aren't needed locally, except as a means to creating our subset object.
Is there a way to accomplish the same thing in a single statement, without introducing the unnecessary locals a and c?
There is no specific syntax for this. You can keep doing:
var subsetObj = {a: o.a, c: o.c};
If you have more properties or a variable number of properties, create a helper function.
Related: Is it possible to destructure onto an existing object? (Javascript ES6)

Copy an object in Javascript without using Jquery [duplicate]

This question already has answers here:
How do I correctly clone a JavaScript object?
(81 answers)
Closed 7 years ago.
I want to create a copy of a JavaScript object which has enumerable and non-enumerable properties. I want make a exact replica of the object with all enumerable and non-enumerable properties copied to the new one.
Any help how can it be done?
JSON.parse(JSON.stringify(obj))
You should use Object.create or it's backward compatible counterpart.
if(!Object.create){
Object.create = function(o){
function F(){}; F.prototype = o;
return new F;
}
}
var oldObj = {prop1:'val1', prop2:'val2', prop3:'val3'};
var newObj = Object.create(oldObj);
delete newObj.prop2;
console.log(newObj); console.log(oldObj);

copying an array of objects in javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Copying array by value in javascript
How to copy an array of objects to another array in Javascript?
var qwerty1 = arr;
var qwerty2 = arr;
Both qwerty1 and qwerty2 may look different but point to the same reference.
I read somewhere that "assigning a boolean or string to a variable makes a copy of that value, while assigning an array or an object to a variable makes a reference to the value." So my two arrays post different operations return the same objects.
Any light in this regard?
The idiomatic way to copy an array in Javascript is to use concat:
var qwerty1 = arr.concat();
var qwerty2 = arr.concat();

Categories