Extracting and combining array elements for a table row

I don’t know why this is so hard for me to figure out and I’m looking forward to one of you kind people to show me just how stupid I am…
I’ve got 3 aggregates with 4 groups. I end up with 3 arrays that look something like this:

[{“type”:A, “all”: 5},{“type”:B, “all”:2},{“type”:C, “all”:3…etc.]
[{“type”:A, “past”: 5},{“type”:B, “past”:2},{“type”:C, “past”:3…etc.]
[{“type”:A, “future”: 5},{“type”:B, “future”:2},{“type”:C, “future”:3…etc.]

I’m attempting to put all of the type A’s, B’s, C’s and D’s into one row so I can display in a table. So each object would look something like:
[{“type”:A, “all”:5, “past”: 5, “future”: 5}]

Why am I struggling with this? If I just combine the 3 original arrays the table duplicates the type in the first column.

Are the arrays always the same size?

If they are always guaranteed to be the same size then you can use something like this:

let a = [{"type":"A", "all": 5},{"type":"B", "all":2},{"type":"C", "all":3}]

let b = [{"type":"A", "past": 5},{"type":"B", "past":2},{"type":"C", "past":3}]

let c = [{"type":"A", "future": 5},{"type":"B", "future":2},{"type":"C", "future":3}]

for(let i = 0; i < a.length; i++) {
  a[i].past = b[i].past
  a[i].future = c[i].future
}

console.log(a)

If the size of the arrays could change then you can just simply check if an index in either b or c exists before adding it to the a array. That would look something like this

if(b[i].past !== undefined) {
	a[i].past = b[i].past
}

// do the same for the c array

That’s a nice answer if you assume that first array contains all the types .
But if that’s not the case, you can do something like:

const a = [{"type": "A", "all": 5},{"type":"B", "all":2},{"type":"C", "all":3}];
const b = [{"type": "A", "past": 5},{"type":"B", "past":2},{"type":"C", "past":3}];
const c = [{"type":"A", "future": 5},{"type":"B", "future":2},{"type":"C", "future":3}];
const allArrays = [{id: 'all', val: a}, {id: 'past', val: b}, {id: 'future', val: c}];
let allTypes = allArrays.flat().map(e => e.val.map(el => el.type)).flat();
allTypes = allTypes.filter((e, i) => allTypes.indexOf(e) === i);
const arrByTypes = allTypes.map(e => ({type: e}));
allArrays.forEach(arr => {
arr.val.forEach(e => {
 let newArrObj = arrByTypes.find(o => o.type === e.type);
 newArrObj[arr.id] = e[arr.id];
})
})
console.log(arrByTypes);
1 Like

Thank you both for your replies and solutions. I was able to find something that works. I haven’t fully vetted it for error checks just yet, but it seems to be working. Here’s the code:

let testArray = [];

let allSails = await allSailSessions ( email , contractDate );
let pastSails = await pastSailSessions ( email , startDate );
let reservedSails = await reservedSailSessions ( email , contractDate );

let mergedArray = [… allSails ,… pastSails ,… reservedSails ];

let typeArray = [ “WA” , “WP” , “WEA” , “WEP” ];
for ( var i = 0 ; i < typeArray . length ; i ++){
let filtered = mergedArray . filter ( x => x . type == typeArray [ i ]);
let assignedObject = Object . assign ( filtered [ 0 ], filtered [ 1 ], filtered [ 2 ]);
testArray . push ( assignedObject );
}

$w ( “#table1” ). rows = testArray ;