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
}
}
}
Related
I have two JSON objects like so:
var obj1 = {a: "apple", b: "banana", c: "carrot"}
var obj2 = {a: "apple", e: “egg” b: "banana", c: "carrot", d: "dog"}
I want to be able to have a Boolean check comparing the two objects without having to remove data from either one. For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.
However, lets say obj1 stays the same but obj2 is the following:
var obj1 = {a: "apple", b: "banana", c: "carrot"}
var obj2 = {a: "ant", e: “egg” b: "banana", c: "carrot", d: "dog"}
With this data, it should return false because the a key's value is not matching even though other fields are matching and some fields are not present in both objects.
Any ideas? I would prefer to use Lodash since they have a quality library. Originally was trying to use _.isEqual(obj1, obj2) but it doesn't work for my exact needs.
I don't know if lodash has such a method. But you can achieve the same result by just utilizing Object.etnries and Array.every methods.
var obj1 = {
a: "apple",
b: "banana",
c: "carrot"
}
var obj2 = {
a: "apple",
b: "banana",
c: "carrot",
d: "dog",
e: "egg"
}
var obj3 = {a: "apple", b: "banana", c: "carrot"}
var obj4 = {a: "ant", e: "egg" ,b: "banana", c: "carrot", d: "dog"}
function checkEquality(a, b) {
const entries1 = Object.entries(a);
const entries2 = Object.entries(b);
const short = entries1.length > entries2 ? entries2 : entries1; // pick the shorter array
const long = short === entries1 ? b : a;
const isEqual = short.every(([k, v]) => long[k] === v);
return isEqual;
}
console.log(checkEquality(obj1, obj2))
console.log(checkEquality(obj3, obj4))
_.isMatch can be of help. Check the below snippet
var obj1 = {
a: "apple",
b: "banana",
c: "carrot"
}
var obj2 = {
a: "apple",
e: "egg",
b: "banana",
c: "carrot",
d: "dog"
}
console.log(_.isMatch(obj2, obj1));
var obj1 = {
a: "apple",
b: "banana",
c: "carrot"
}
var obj2 = {
a: "ant",
e: "egg",
b: "banana",
c: "carrot",
d: "dog"
}
console.log(_.isMatch(obj2, obj1));
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.20/lodash.min.js"></script>
Note that _.isMatch(obj1, obj2) will not work. You will need to put some logic to determine which object has more keys and pass it as first argument.
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)
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)
This question already has answers here:
Extract certain properties from all objects in array
(5 answers)
Closed 4 months ago.
I have an array of Object as follows:
var obj = [
{a: 1, b: 5, c: 9},
{a: 2, b: 6, c: 10},
{a: 3, b: 7, c: 11},
{a: 4, b: 8, c: 12}
];
I know about how to get single object using Array.map() like this.
var result = obj.map(x=>x.a)
This will give me following result
[1, 2, 3, 4]
But I want result like follows:
[
{a: 1, b: 5},
{a: 2, b: 6},
{a: 3, b: 7},
{a: 4, b: 8}
]
In short, from an array of objects I want to select only a few fields (more than one).
How can I do that?
You can use .map() with Object Destructuring:
let data = [
{a:1,b:5,c:9}, {a:2,b:6,c:10},
{a:3,b:7,c:11}, {a:4,b:8,c:12}
];
let result = data.map(({ a, b }) => ({a, b}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If, as in your example, you want to exclude a particular property or few, you can use destructuring and use rest properties to create an object with only the properties you want:
var obj = [
{a:1,b:5,c:9},
{a:2,b:6,c:10},
{a:3,b:7,c:11},
{a:4,b:8,c:12}
];
const mapped = obj.map(({ c, ...rest }) => rest);
console.log(mapped);
If you want to include properties, simply extract them from the .map callback:
var obj = [
{a:1,b:5,c:9},
{a:2,b:6,c:10},
{a:3,b:7,c:11},
{a:4,b:8,c:12}
];
const mapped = obj.map(({ a, b }) => ({ a, b }));
console.log(mapped);
Use map():
var data = [
{a:1,b:5,c:9},
{a:2,b:6,c:10},
{a:3,b:7,c:11},
{a:4,b:8,c:12}
];
let modified = data.map(obj => ({a: obj.a, b: obj.b}))
console.log(modified);
Or if you prefer destructuring:
var data = [
{a:1,b:5,c:9},
{a:2,b:6,c:10},
{a:3,b:7,c:11},
{a:4,b:8,c:12}
];
let modified = data.map(({ a, b }) => ({a, b}));
console.log(modified);
var obj = [
{a: 1, b: 5, c: 9},
{a: 2, b: 6, c: 10},
{a: 3, b: 7, c: 11},
{a: 4, b: 8, c: 12}
];
var result = obj.map(e => ({a:e.a , b:e.b}));
console.log(result)
You can return a custom object with required properties using map()
var obj = [{a:1,b:5,c:9},
{a:2,b:6,c:10},
{a:3,b:7,c:11},
{a:4,b:8,c:12}
];
let op = obj.map(e => {
return { a:e.a, b: e.b };
})
console.log(op);
In your solution for producing [1,2,3,4], x.a isn’t some micro-syntax, it’s actually a full-fledged JavaScript expression. So you can just replace it with the JavaScript for creating a new object with the properties you want.
var result = obj.map(x=>{a: x.a, b: x.b});
... almost. The additional complication is that a { after a => is interpreted as the beginning of a function body, not the beginning of an object literal. You can avoid this by just wrapping the object literal in otherwise-noop parenthesis.
var result = obj.map(x=>({a: x.a, b: x.b}));
You can make your own custom function for this, and pass it a set of properties which you want to extract. :
var array = [{a:1,b:5,c:9}, {a:2,b:6,c:10}, {a:3,b:7,c:11}, {a:4,b:8,c:12} ];
function extractProperties(arr, properties){
return arr.map((obj)=> Object.keys(obj).reduce((acc,key)=>{
if(properties.has(key))
acc[key] = obj[key];
return acc;
},{}));
}
let set = new Set(["a","b"]);
let result = extractProperties(array, set);
console.log(result);
set.add("c");
console.log("**************result2**********")
let result2 = extractProperties(array, set);
console.log(result2);
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);