0

I have an array that looks like this:

var arr = [
[
    "2021-07-31T00:00:00Z",
    "648429a0-00e5-4752-9d84-2857a0ea0787"
],
[
    "2021-08-31T00:00:00Z",
    "648429a0-00e5-4752-9d84-2857a0ea0787"
],
[
    "2021-07-31T00:00:00Z",
    "AAAA"
],
[
    "2021-08-31T00:00:00Z",
    "BBBB"
]

]

I'd like to transform this based on the first value (the date) of each array. So if the dates match they will merge into one. So the output I'm trying to get is

[
[
    "2021-07-31T00:00:00Z",
    "648429a0-00e5-4752-9d84-2857a0ea0787",
    "AAAA"
],
[
    "2021-08-31T00:00:00Z",
    "648429a0-00e5-4752-9d84-2857a0ea0787",
     "BBBB"
]

]

Would be grateful to know what would be the best approach in this instance.

5
  • please add your code. what does not work? Commented Nov 22, 2021 at 18:26
  • Use an object whose keys are the first values. Commented Nov 22, 2021 at 18:30
  • This is as far as I got but I think the issue is that I'm really stuck on how to best approach it as opposed to code not working. uniqueDates.forEach((date) => { arrMaster.forEach((item) => { if(item[0] == date){ // do something } }) } Commented Nov 22, 2021 at 18:33
  • @barmar - could I please ask you to elaborate or point me to a page that might outline what you mean? many thanks Commented Nov 22, 2021 at 20:50
  • There are hundreds of questions here about merging arrays. Even if you can't find one that's exactly the same, the general ideas should be the same and you should be able to figure it out. Commented Nov 22, 2021 at 20:52

3 Answers 3

1

Use a map to overwrite the values as they are found in the array. Then use Object.entries() to create the final array:


var original = [ [1,2], [1,3], [2,3]]
var final = []
var map = {}

original.forEach(a => {
    if (!map[a[0]]) { map[a[0]] = [] }
    map[a[0]].push(a[1]);
});
Object.entries(map).forEach(e => final.push([e[0], ...e[1]]))

Edit: Changed the answer to get all the values

Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks for this. I'm finding however that the output shows as [ [ "1", 3 ], [ "2", 3 ] ]. Is there a way for the array to output like the below (so that both values are included)? [ [ "1", 2, 3 ], [ "2", 3 ] ]
You just need to modify the way data is stored in the map. Edited the answer to reflect this
This works perfectly! Thanks again - will be very useful
@user2547766 ... though the above approach works, it depends on references outside of the forEach's callback function. In addition it needs two other full iteration cycles (Object.entries and forEach) in order to accumulate the final result, and, here again, the forEach callback needs to know the final reference which is from an outer scope. Thus such approaches never provide reusable functionality.
0

You can try this reduce approach

    var arr = [
    [
        "2021-07-31T00:00:00Z",
        "648429a0-00e5-4752-9d84-2857a0ea0787"
    ],
    [
        "2021-08-31T00:00:00Z",
        "648429a0-00e5-4752-9d84-2857a0ea0787"
    ],
    [
        "2021-07-31T00:00:00Z",
        "AAAA"
    ],
    [
        "2021-08-31T00:00:00Z",
        "BBBB"
    ]
];

arr = arr.reduce((a, i) => {
    if (!a) a = [];
    var found = false;
    a.forEach(ai => {
        if (ai[0] == i[0]) {
            found = true;
            for (var k = 0; k < i.length; k++) {
                if (k > 0)
                    ai.push(i[k]);
            }
        }
    });
    if (!found)
        a.push(i);
    return a;
}, []);

Comments

0

The OP's task can be achieved without nested iterations within a single reduce cycle.

The underlying approach is to use a tailored object as accumulator/collector. Such a collector features 2 properties ... index which serves as Object based map/lookup for grouped arrays where the group key is always the 1st item of each (to be) processed array ... and ... result which is going to hold references of the above mentioned (yet to be created) grouped arrays where one does always push the 2nd item of the currently processed array into.

var arr = [[
  "2021-07-31T00:00:00Z",
  "648429a0-00e5-4752-9d84-2857a0ea0787",
], [
  "2021-08-31T00:00:00Z",
  "648429a0-00e5-4752-9d84-2857a0ea0787",
], [
  "2021-07-31T00:00:00Z",
  "AAAA",
], [
  "2021-08-31T00:00:00Z",
  "BBBB",
]];

function collectAndAggregateGroupedArrays(collector, item) {
  const { index, result } = collector;

  let groupKey = item[0];
  let groupedList = index[groupKey];

  if (!groupedList) {
    groupedList = index[groupKey] = [groupKey];

    result.push(groupedList);
  }
  groupedList.push(item[1]);

  return collector;
}
console.log(
  arr.reduce(collectAndAggregateGroupedArrays, {

    index: {},
    result: [],

  }).result
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

1 Comment

@user2547766 ... you might check out the late above provided approach/solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.