JavaScript Summerize Array with Arrays in Objetcs with different Labels

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']
}]
Answer:1

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);
Answer:2

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)
Answer:3

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?

  1. need some explanation
  2. we need some explanation
  3. he need some milk explanation
  4. some things don't need an explanation
  5. need some explanation traduction

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 ...

  1. string_split second value
  2. split second string
  3. split string at second space python
  4. split string on second space
  5. split string at second space javascript
  6. split string by second space java
  7. split string at second space c#
  8. split string after second space c#
  9. split string after second space python
  10. split string after second space
  11. sql split string second value
  12. split string and get second element in java
  13. split string and get second value in c#
  14. c# string split second element
  15. java string split second argument
  16. string_split select second value
  17. split string and get second value in javascript
  18. split string and get second element in c#
  19. split string and get second element in javascript
  20. python split string on second occurrence

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?

  1. convert htmlelement string
  2. convert htmlelement to string javascript
  3. convert html element to string
  4. convert html element to string typescript
  5. convert html element to string jquery
  6. convert htmlelement to string python
  7. convert html element into string
  8. convert html element to string online
  9. convert object htmlelement to string
  10. angular convert htmlelement to string
  11. typescript convert htmlelement to string
  12. convert jquery htmlelement to string