compare array of object with array of object in javascript - javascript

compare array of object with array of keys, filter array of object with array keys.
Input:
let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
output:
b = [{bb: 2, c: 30 },{bb: 3, c: 40}];
original array should be mutate.

Much similiar to #SachilaRanawaka 's answer, but works without modifying the original b array:
let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
function removeKey(obj, key) {
let clone = Object.assign({}, obj); // <-- shallow clone
if (key in clone) {
delete clone[key];
}
return clone;
}
function removeKeys(keys, objs) {
return objs.map(o => keys.reduce(removeKey, o));
}
console.log(removeKeys(a, b));

You could take a destructuring with getting the rest approach.
This approach does not mutate the original data.
const
unwanted = ['aa'],
data = [{ aa: 1, bb: 2, c: 30 }, { aa: 2, bb: 3, c: 40 }],
result = data.map(o => unwanted.reduce((q, k) => {
const { [k]: _, ...r } = q;
return r;
}, o));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can simply achieve this requirement with the help of Array.forEach() method.
Live Demo :
let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
b.forEach(obj => {
Object.keys(obj).forEach(key => {
a.forEach(item => delete obj[item])
});
});
console.log(b);

It can probably be solved with less lines of code, but this was the first i could think of.
let keysToRemove = ['aa'];
let array = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
let result = array.map((item) => {
let filtered = Object.keys(item)
.filter((key) => !keysToRemove.includes(key))
.reduce((obj, key) => {
obj[key] = item[key];
return obj;
}, {});
return filtered;
});
console.log(result);

use the map operator and use delete to delete properties from the object
let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
const result = b.map(item => {
Object.keys(item).forEach(key => {
if(a.includes(key)){
delete item[key]
}
})
return item
})
console.log(result)

Related

If object has specific properties create a new object only with them

As always I will explain my problem by example (that I solved but its a lot of code and its ugly, that's why I'm looking for a better solution). I'm trying to look at an object like this:
const object1 = {
a: {a:1},
b: 2,
c: 3,
d: 4,
};
I want to check if this object has any of the following properties [a,f] and if have one of them to create a new object with these properties
const object2 = {
a: {a:1},
};
const object1 = {
a: {a:1},
b: 2,
c: 3,
d: 4,
}
const arrOfItem = ['a', 'd']
const newObj = {}
for(let item in object1) {
if(arrOfItem.includes(item)) {
newObj[item]= object1[item]
}
}
console.log(newObj)
see if this works for you,
function makeObject (properties) {
const originalObject = {
a: {a:1},
b: 2,
c: 3,
d: 4,
};
let newObject = {}
properties.forEach(property => {
if(originalObject.hasOwnProperty(property)) {
newObject[property] = originalObject[property];
}
});
return newObject;
}
pass the properties as an array of strings to makeObject function
const d = ['a', 'f', 'd']
const object1 = {
a: {a:1},
b: 2,
c: 3,
d: 4,
};
const object2 = d.reduce((acc, ele) => {
if(object1[ele] !== undefined) acc[ele] = object1[ele];
return acc;
}, {});
console.log(object2);

Extracting key value from object into array of objects with specific fields

So I have this data:
fields = ['a', 'b', 'c']
data = [{r: 1, a: 2, b: 3, c: 4, h: 5}, {r: 4, a: 9, b: 1, c: 4, h: 5} ... ]
and I want to be able (preferred with lodash) to be able to get to this:
newData = [{r:1, h:5, values: [{name: 'a', value: 2},{name: 'b', value: 3}, {name: 'c', value: 4}], .....]
Meaning only the fields from the 'fields' object be taken out of each object in array (they always exist) and put into 'values' property that has an array of them in the format displayed here.
Would love to hear suggestions of the cleanest way to achieve this!
I did this :
function something(data, fields) {
const formattedData = _.map(data, (currData) => {
const otherFields = _.omit(currData, fields)
return {
...otherFields,
values: _.flow(
currData => _.pick(currData, fields),
pickedFields => _.toPairs(pickedFields),
pairs => _.map(pairs, pair => {
return { name: pair[0], value: pair[1] }
})
)(currData)
}
})
return formattedData
}
which works, but I'm wondering if it isn't a bit complicated.
The _.flow() method creates a function, which you can extract and name. In addition, the 1st function in the flow, accepts more than 1 parameter, so you don't need to pass it explicitly. Since _.toPairs() is unary, you don't need to wrap it in an arrow function.
The object creation is a bit annoying. I've used _.zipObject(), but it's still cumbersome.
Now you can use the function create by _.flow() in your main function, and it's pretty readable:
const { flow, pick, toPairs, map, partial, zipObject, omit } = _
const propsToObjs = flow(
pick,
toPairs,
pairs => map(pairs, partial(zipObject, ['name', 'value'])),
)
const fn = (data, fields) =>
map(data, currData => ({
...omit(currData, fields),
values: propsToObjs(currData, fields)
}))
const fields = ['a', 'b', 'c']
const data = [{r: 1, a: 2, b: 3, c: 4, h: 5}, {r: 4, a: 9, b: 1, c: 4, h: 5}]
const result = fn(data, fields)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
Using lodash/fp, we can make the flow function even nicer, since lodash/fp functions are auto-curried and iteratee-first data-last (not the reversed order of parameters):
const { flow, pick, toPairs, map, partial, zipObject, omit } = _
const propsToObjs = flow(
pick,
toPairs,
map(zipObject(['name', 'value']))
)
const fn = fields => map(currData => ({
...omit(fields, currData),
values: propsToObjs(fields, currData)
}))
const fields = ['a', 'b', 'c']
const data = [{r: 1, a: 2, b: 3, c: 4, h: 5}, {r: 4, a: 9, b: 1, c: 4, h: 5}]
const result = fn(fields)(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash#4(lodash.min.js+lodash.fp.min.js)'></script>
You could map through the objects in data and then check if the key is in the fields array:
fields = ["a", "b", "c"];
data = [
{ r: 1, a: 2, b: 3, c: 4, h: 5 },
{ r: 4, a: 9, b: 1, c: 4, h: 5 },
];
let newData = data.map((o) => {
let newObject = {};
newObject.values = [];
for (let k in o) {
if (fields.includes(k)) {
newObject.values.push({
name: k,
value: o[k]
});
} else {
newObject[k] = o[k];
}
}
return newObject;
});
console.log(newData);
You could destructure the object, pick the wanted properties and return the rest of the object with wanted values.
const
fields = ['a', 'b', 'c'],
data = [{ r: 1, a: 2, b: 3, c: 4, h: 5 }, { r: 4, a: 9, b: 1, c: 4, h: 5 }],
result = data.map(o => {
const values = fields.map(name => {
let value;
({ [name]: value, ...o } = o);
return { name, value };
});
return { ...o, values };
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Change the array and return a new one

I have an array with objects. I filtered the array and returned the values that correspond with a condition. After that i created a new array what contains data from first. This array contains a value a which should overrides the first array.
const arr = [{
a: 1,
b: 2,
name: 'one'
},
{
a: 1,
b: 7,
name: 'two'
}
]
const b = [1, 2]
const res = arr.filter(i => b.includes(i.b))
const newArr = {
...res,
a: 'newdata'
}
console.log(newArr)
In console i got:
{
"0": {
"a": 1,
"b": 2,
"name": "one"
},
"a": "newdata"
}
But the expected output is:
{
"a": "newdata"
"b": 2,
"name": "one"
},
Why i get this and how to get the expected result?
After you filter the values you're interested in, you could use Array.map(..) to go through all the items and change their a property, here is an example:
const arr = [{
a: 1,
b: 2,
name: 'one'
},
{
a: 1,
b: 7,
name: 'two'
}
]
const b = [1, 2]
const res = arr.filter(i => b.includes(i.b)).map(i => ({...i, a: 'newdata'}));
console.log(res)
You simply spread an array and you get the indices from the array/object as key and the item/object as value.
Instead, you could take a function for a new object with additional key/value and map the filtered result by using the function.
const
arr = [{ a: 1, b: 2, name: 'one' }, { a: 1, b: 7, name: 'two' }],
b = [1, 2, 7],
res = arr.filter(i => b.includes(i.b)),
newObject = o => ({ ...o, a: 'newdata' }),
newArr = res.map(newObject);
console.log(newArr);

How can I uniquely union two array of objects?

I am trying to merge two arrays of objects without using the unionBy method from lodash.
Currently I have the following code working perfectly:
var array1 = [
{ a: 1, b: 'first'},
{ a: 2, b: 'second'}
];
var array2 = [
{ a: 3, b: 'third'},
{ a: 1, b: 'fourth'}
];
var array3 = __.unionBy(array2, array1, 'a');
This outputs:
[
{
"a": 3,
"b": "third"
},
{
"a": 1,
"b": "fourth"
},
{
"a": 2,
"b": "second"
}
]
This is the desired result but I can't use unionBy in my current work environment, so I'm looking for a result that uses either native JS or other lodash methods 3.6.0 or lower.
Concat and use Array#filter with a helper object to remove duplicates:
var array1 = [{"a":1,"b":"first"},{"a":2,"b":"second"}];
var array2 = [{"a":3,"b":"third"},{"a":1,"b":"fourth"}];
var result = array2.concat(array1).filter(function(o) {
return this[o.a] ? false : this[o.a] = true;
}, {});
console.log(result);
If ES6 is an option you can use a Set instead of the helper object:
const array1 = [{"a":1,"b":"first"},{"a":2,"b":"second"}];
const array2 = [{"a":3,"b":"third"},{"a":1,"b":"fourth"}];
const result = array2.concat(array1).filter(function(o) {
return this.has(o.a) ? false : this.add(o.a);
}, new Set());
console.log(result);
If you want to use an arrow function, you can't use the thisArg of Array.filter() to bind the Set as the this of the function (you can't bind this to arrow functions). You can use a closure instead (attribute for the method goes to #NinaScholz).
const array1 = [{"a":1,"b":"first"},{"a":2,"b":"second"}];
const array2 = [{"a":3,"b":"third"},{"a":1,"b":"fourth"}];
const result = [...array2, ...array1]
.filter((set => // store the set and return the actual callback
o => set.has(o.a) ? false : set.add(o.a)
)(new Set()) // use an IIFE to create a Set and store it set
);
console.log(result);
You could take a Set for filtering to get unique values.
var array1 = [{ a: 1, b: 'first' }, { a: 2, b: 'second' }],
array2 = [{ a: 3, b: 'third' }, { a: 1, b: 'fourth' }],
s = new Set,
array3 = array2.map(o => (s.add(o.a), o)).concat(array1.filter(o => !s.has(o.a)));
console.log(array3);
You can use an ES6 Map for this. Construct it with the data, keyed by the a property value, and then take the values out of the Map again:
var array1 = [{"a":1,"b":"first"},{"a":2,"b":"second"}],
array2 = [{"a":3,"b":"third"},{"a":1,"b":"fourth"}];
var result = [...new Map([...array1,...array2].map( o => [o.a, o] )).values()];
console.log(result);
You can merge the 2 arrays and then filter the ones with same property a:
var array1 = [{ a: 1, b: 'first'},{ a: 2, b: 'second'}],
array2 = [{ a: 3, b: 'third'},{ a: 1, b: 'fourth'}],
array3 = [...array2, ...array1].filter((item, pos, arr) =>
arr.findIndex(item2 => item.a == item2.a) == pos);
console.log(array3)
If you want to still be able to specify the property by which to union you can implement you own function like this:
var array1 = [{ a: 1, b: 'first'},{ a: 2, b: 'second'}],
array2 = [{ a: 3, b: 'third'},{ a: 1, b: 'fourth'}],
array3 = unionBy(array1, array2, 'a');
function unionBy(array1, array2, prop){
return [...array2, ...array1].filter((item, pos, arr) =>
arr.findIndex(item2 => item[prop] == item2[prop]) == pos);
}
console.log(array3);
Note: One advantage of my answer over some of the answers is that it preserves the order like in lodash which may or may not be important.
ES5 using Array.filter and Array.find
var array1 = [{ a: 1, b: "first" }, { a: 2, b: "second" }];
var array2 = [{ a: 3, b: "third" }, { a: 1, b: "fourth" }];
function merge(a, b, prop) {
var reduced = a.filter(function(itemA) {
return !b.find(function(itemB) {
return itemA[prop] === itemB[prop];
});
});
return reduced.concat(b);
}
console.log(merge(array1, array2, "a"));
ES6 arrow functions
var array1 = [{ a: 1, b: "first" }, { a: 2, b: "second" }];
var array2 = [{ a: 3, b: "third" }, { a: 1, b: "fourth" }];
function merge(a, b, prop) {
const reduced = a.filter(
itemA => !b.find(itemB => itemA[prop] === itemB[prop])
);
return reduced.concat(b);
}
console.log(merge(array1, array2, "a"));
Another ES6 one line experiment
var array1 = [{ a: 1, b: "first" }, { a: 2, b: "second" }];
var array2 = [{ a: 3, b: "third" }, { a: 1, b: "fourth" }];
const merge = (a, b, p) => a.filter( aa => ! b.find ( bb => aa[p] === bb[p]) ).concat(b);
console.log(merge(array1, array2, "a"));
You could use ES6 find and reduce function smartly!
var array1 = [{"a":1,"b":"first"},{"a":2,"b":"second"}];
var array2 = [{"a":3,"b":"third"},{"a":1,"b":"fourth"}];
var res = array1.concat(array2).reduce((aggr, el)=>{
if(!aggr.find(inst=>inst.a==el.a))
return [...aggr, el];
else
return aggr
},[])
console.log(res);

Hot to use reduce to convert a array of objects to a single object

i'm trying to work with reduce or map, but i'm a noob some times.
i'm trying to use this function to return a single array from the objects.
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
var result = obj.reduce((obj, item) => [item.key] = item.value);
console.log(result);
but i'm always getting :
Uncaught TypeError: Cannot read property 'Symbol(Symbol.iterator)'
of undefined
I searched a lot, but the examples didn't help me... i think that's something simple, but after 1 hour, i'm nothing getting .
What i want..
[{a: 1}, {b: 2}, {c: 3}] to {a: 1, b: 2, c: 3}
You could use Object.assign and spread syntax ....
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(Object.assign({}, ...obj));
With Array#reduce
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(obj.reduce((r, o) => Object.assign(r, o), {}));
Without Object.assign
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(obj.reduce((r, o) => (Object.entries(o).forEach(([k, v]) => r[k] = v), r), {}));
ES5
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
console.log(obj.reduce(function (r, o) {
Object.keys(o).forEach(function (k) {
r[k] = o[k];
});
return r;
}, {}));
If you want to use reduce:
var arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
var result = arr.reduce((obj, item) => Object.assign(obj, item), {});
Check the MDN documentation when in doubt.
you can do it in the following way using reduce
var obj = [{ a: 1 }, { b: 2 }, { c: 3 }];
var result = obj.reduce((obj, item) => {
Object.assign(obj, item)
return obj;
}, {});
console.log(result);

Categories