I have an array called p which looks like:
[
{
"Day": "Monday",
"data": [
"chest",
"triceps",
"shoulders"
]
},
{
"Day": "Tuesday",
"data": [
"back",
"biceps"
]
},
{
"Day": "Thursday",
"data": [
"legs"
]
}
]
and I have another array, program which looks like: (skimmed down)
[
{
"target": "biceps",
"data": [
{
"name": "barbell alternate biceps curl",
"target": "biceps"
},
{
"name": "barbell lying preacher curl",
"target": "biceps"
}
]
},
{
"target": "triceps",
"data": [
{
"name": "barbell incline reverse-grip press",
"target": "triceps"
},
{
"name": "barbell lying extension",
"target": "triceps"
}
]
}
]
I need program to group the muscle groups that p.data has. It should look like:
[
{
"target": "chest tricep shoulder",
"data": [
{
"name": "chest exercise",
"target": "chest"
},
{
"name": "tricep exercise",
"target": "tricep"
},
{
"name": "shoulder exercise",
"target": "shoulder"
},
]
},
{
"target": "back biceps",
"data": [
{
"name": "back exercise",
"target": "back"
},
{
"name": "bicep exercise",
"target": "bicep"
},
]
},
]
I have tried this (sort based off array), but not the right attempt:
function mapOrder(array, order, property) {
let ordered = [], unordered = [];
// Iterate over each item in the supplied array of objects, separating ordered and unordered objects into their own arrays.
array.forEach((item) => {
if (order.indexOf(item[property]) === -1) {
unordered.push(item);
} else {
ordered.push(item);
}
});
// Sort the ordered array.
ordered.sort((a, b) => {
a = a[property], b = b[property];
if (order.indexOf(a) < order.indexOf(b)) {
return -1;
} else {
return 1;
}
});
// Sort the unordered array.
unordered.sort((a, b) => {
a = a[property], b = b[property];
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
// Append the sorted, non-ordered array to the sorted, ordered array.
ordered.push(...unordered);
return ordered;
}