I have the following objects like
obj1 = { key1: { a: 1}}
I want to merge the following object with the above
key1 = { b: 2}
I want to get the result as following by merging the key1 with the existing key in the first object.
{key1: {a: 1, b: 2}}
How can I do this using spread operator in javascript? Thanks in advance.
You can spread both existing and new object
Note: Using spread operator will only merge the enumerable properties.
const obj1 = { key1: { a: 1}}
const key1 = { b: 2};
const res = {...obj1,key1:{...obj1.key1,...key1}};
console.log(res)
If you want to modify the original object then only change obj.key1
const obj1 = { key1: { a: 1}}
const key1 = { b: 2};
obj1.key1 = {...obj1.key1,...key1}
console.log(obj1)
Just use the spread operator like so:
let obj1 = {
key1: {
a: 1
}
};
let key1 = {
b: 2
};
obj1.key1 = { ...obj1.key1, ...key1 };
console.log(obj1);
.as-console-wrapper { max-height: 100% !important; top: auto; }
You could splead it into the wanted property.
var obj1 = { key1: { a: 1 } },
key1 = { b: 2 };
obj1.key1 = { ...obj1.key1, ...key1 };
console.log(obj1);
Try this:
let obj1 = { key1: { a: 1}}
let b = { b: 2}
let a = obj1.key1; //{a:1}
// merge using spread operator
let key1 = {...a, ...b}; //{a:1, b:2}
obj1.key1 = key1; //{"a":1,"b":2"}
console.log(obj1) //{ "key1":{"a":1,"b":2"}}
You should use Object.assign
var obj1 = { key1: { a: 1}};
var key1 = { b: 2};
Object.assign(obj1.key1, key1);
console.log(obj1)
Related
I have array of objects like this
const object1 = {
a: 'somestring',
b: 42,
c: false
};
const object2 = {
a: 'somestring2',
b: 42,
c: false
};
const object3 = {
a: 'somestring3',
b: 42,
c: false
};
const arr = [object1,object2,object3]
i want to get all the values of 'a' key.
so result be
['somestring','somestring2','somestring3']
I tried Object.values() but it gets me all values of all keys, which is not the desired output.
Just use map() function:
const arr = [object1,object2,object3].map(({a}) => (a))
An example:
const object1 = {
a: 'somestring',
b: 42,
c: false
};
const object2 = {
a: 'somestring2',
b: 42,
c: false
};
const object3 = {
a: 'somestring3',
b: 42,
c: false
};
const arr = [object1,object2,object3].map(({a}) => (a))
console.log(arr)
Have you tried const arr = [object1.a, object2.a, object3.a]? That might work
how can i return an array of objects taking from array of again one more level array. I am using push.
is there any better way to achieve this
let a = [{b: [{c: "k"}]}]
let o = []
a.forEach(so => {so.b.forEach(obc => o.push(obc))})
console.log(o)
I'd use flatMap() instead:
const a = [{
b: [{
foo: 'foo'
}]
},
{
b: [{
c: "k"
},
{
bar: 'bar'
}
]
}
];
const o = a.flatMap(({ b }) => b);
console.log(o);
(but this is a relatively new method, so if you want to use it and still support older environments, be sure to include a polyfill)
Lacking that, you can also improve your existing code by using concat() with the inner array instead of iterating over each inner item:
const a = [{
b: [{
foo: 'foo'
}]
},
{
b: [{
c: "k"
},
{
bar: 'bar'
}
]
}
];
let o = [];
a.forEach(({ b }) => {
o = o.concat(b);
});
console.log(o);
Try
let a = [{b: [{c: "k"}]}]
let o =a[0].b
console.log(o)
Here is my code:
const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc',
b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
result[index] = item
return result;
}, {});
let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
value[dealId] = array2.push(obj[property]);
}
console.log(value)
The output of this is
Object { 123: 3 }
But I want and this is what I was expecting.
Object { 123: [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', b: 'mno'}] }
Why am I getting 3 instead of an array? How to get the array?
Array.prototype.push returns the new length of the array, not the array itself. Your loop assigns the values 1, 2 and 3 to the same value[dealId].
Instead, you can move the assignment outside the loop:
for (var property in obj) {
array2.push(obj[property]);
}
value[dealId] = array2;
Or you can simply use Object.values:
value[dealId] = Object.values(obj);
But note that this does not list inherited properties.
Why not build a new object with a single key and the given array as value?
For the object use a computed property name.
const
array = [{ a: 'abc', b: 'cd' }, { a: 'abc', b: 'xyz' }, { a: 'abc', b: 'mno' }],
key = 123,
object = { [key]: array };
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
you need to add into aaray first then create the object
const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc', b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
result[index] = item
return result;
}, {});
let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
array2.push(obj[property]);
}
value[dealId] = array2;
console.log(value);
const array1 = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc',
b: 'mno'}];
let obj = array1.reduce(function(result, item, index){
result[index] = item
return result;
}, {});
let dealId = 123;
let value = {};
let array2 = [];
for (var property in obj) {
array2.push(obj[property]);
}
value[dealId] = array2;
console.log(value)
Assign your new obj property (id) the requested array:
let arr = [{a: 'abc', b: 'cd'}, {a: 'abc', b: 'xyz'}, {a: 'abc',
b: 'mno'}];
let newObj = {};
let id = 123;
newObj[id] = arr;
console.log(newObj);
Anyone know how I could further condense my mergePath method, which merges keys on two objects based on a key/value path? My solution below uses Lodash.
let obj1 = { z: {fields: { a: "200", b: "2" }}}
let obj2 = { z: {fields: { a: "2", b: "20" }}}
let objsPath = "z.fields"
let mergePath = (objsPath, obj1, obj2) => (
_.set(obj1, objsPath, {..._.get(obj1, objsPath), ..._.get(obj2, objsPath)})
)
You can use _.merge() to copy the path from obj2 to obj2. Then you can return obj1 (I've used the comma operator):
const obj1 = { z: {fields: { a: "200", b: "2" }}}
const obj2 = { z: {fields: { a: "2", b: "20" }}}
const objsPath = "z.fields"
const mergePath = (objsPath, obj1, obj2) => (
_.merge(_.get(obj1, objsPath), _.get(obj2, objsPath)), obj1
)
console.log(mergePath(objsPath, obj1, obj2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
The simplest way without using external library
const obj1 = { z: {fields: { a: "200", b: "2", c: "66" }}}
const obj2 = { z: {fields: { a: "2", b: "20" }}}
const mergedObj = {z: {fields: {}}}
Object.assign(mergedObj.z.fields ,obj1.z.fields, obj2.z.fields)
console.log(mergedObj)
Another way is using object spread operator but you need babel preset to use this feature - This is kind of future syntax. :)
That syntax will be like
const mergedObj = {
z: {
fields: {
...obj1.z.fields,
...obj2.z.fields
}
}
}
Can I create an Array from an Object just in one line? I don't want all the values object, just a selection:
const myObject = { a: 'foo', b: 'bar', c:'yep' }
const { a, c } = myObject
const myArray = Array.of(a, c)
console.log(myArray)
Could I use destructuring in some way inside the Array.of parameter?
Why not just :
const myObject = { a: 'foo', b: 'bar', c:'yep' };
let arr = Array.of(myObject.a, myObject.c);
console.log(arr);