I have an Array with Objects, there some of them has same "Subarrays" but with different labels. I need to summarize those based on equality.
Comparing two different arrays is easy so far, but I am not sure how to do it in the same array. This is my array.
let array = [{
label: 'sting1',
subArray: ['A', 'B']
}, {
label: 'sting2',
subArray: ['B']
}, {
label: 'sting3',
subArray: ['A', 'B']
}]
As you can see, the Objects labeled with String1
and String3
has the same array. So now I need to summarize those. The result I need is is this:
let result = [{
label: ['sting1', 'string3'],
subArray: ['A', 'B']
}, {
label: ['sting2'],
subArray: ['B']
}]
just to give you an option without the map and reduce. When learning javascript I usually preferred to always go back to simple syntax to learn what was happening.
let me know if that helped!
let array = [{label: 'sting1', subArray: ['A', 'B']}, {label: 'sting2',subArray: ['B']}, {label: 'sting3', subArray: ['A', 'B']}]
result = [];
checkdups = [];
for (i = 0; i < array.length; i++) {
data = {
label: [array[i].label],
subArray: array[i].subArray
}
for (var j = 0; j < array.length; j++) {
if (String(array[j].subArray) == String(array[i].subArray) && i != j) {
data.label.push(array[j].label);
}
}
if (checkdups.indexOf(String(array[i].subArray))==-1) {
checkdups.push(String(array[i].subArray));
result.push(data);
}
}
console.log(result);
Assuming your subArray contains only primitive types so no objects, arrays or functions you can create key with sort
and join
methods, then you use reduce
to build the result.
let array = [{"label":"sting1","subArray":["A","B"]},{"label":"sting2","subArray":["B"]},{"label":"sting3","subArray":["A","B"]}]
const map = array.reduce((r, e) => {
const key = e.subArray.sort().join('-');
if (!r.has(key)) r.set(key, { ...e, label: [e.label]});
else r.get(key).label.push(e.label);
return r;
}, new Map)
const result = [...map.values()];
console.log(result)
I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...
I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...
Going through an online test, i found this weird expression where "1"- -"1" = 2 i understand -"1" will be converted to number, but the other "1"- is getting converted too?
Going through an online test, i found this weird expression where "1"- -"1" = 2 i understand -"1" will be converted to number, but the other "1"- is getting converted too?
I need to split a string, but grab the second half of the string... var str = "How, are, you, doing, today?"; var res = str.split(",", 3); console.log(res); Returns "How,are,you." How ...
I need to split a string, but grab the second half of the string... var str = "How, are, you, doing, today?"; var res = str.split(",", 3); console.log(res); Returns "How,are,you." How ...
Is it possible to convert a HTMLElement to a string putting all css styles (including the ones created dynamically with javascript) in style tags?
Is it possible to convert a HTMLElement to a string putting all css styles (including the ones created dynamically with javascript) in style tags?