Link to home
Start Free TrialLog in
Avatar of ocean O
ocean OFlag for United States of America

asked on

Loop an array in Javascript

Hi, I have a question about Javascript.
 I have a array like below:

[2020, 2, 11, 1, E]
[2020, 2, 11, 1, F]
[2020, 2, 14, 1, E]
[2020, 2, 15, 3, G]
[2020, 2, 15, 2, H]
[2020, 2, 15, 1, E]
[2020, 2, 16, 1, E]

I have to loop this array in javascript, and create a new list which based on date.
For the same date, the data should combine into one row.
For example something like this:  
"2020, 2, 11", {1, E}, {1,F}
"2020, 2, 14", {1, E}
"2020, 2, 15", {3, G}, {2, H}, {1, E}
"2020, 2, 16", {1, E}

Any inputs will be appreciated
Avatar of leakim971
leakim971
Flag of Guadeloupe image

var arr = [[2020, 2, 11, 1, "E"], [2020, 2, 11, 1, "F"],....;
var bydate = {};
for(var i=0;i<arr.length;i++) {
    if(!bydate[arr[i]]) {
        bydate[arr[i]] = []; 
    }
    bydate[arr[i]].push({arr[i][3], arr[i][4]});
}

Open in new window

Avatar of ocean O

ASKER

I tried with it, but it can't run. there is errors
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial