I have an array like this:
var data = [
{ id: 537, name: "aBase" },
{ id: 1, name: "aCourt", aBaseId: 537, rating: { x: 6.4, y: 8 } },
{ id: 2, name: "aCourt", aBaseId: 537, rating: { x: 6.4, y: 10 } },
{ id: 3, name: "aCourt", aBaseId: 537, rating: { x: 6.4, y: 5 } },
{ id: 4, name: "aCourt", aBaseId: 537, rating: { x: 6.4, y: 2 } },
];
and I want the id of the 'aBase' object and the min y of all of the 'aCourt' objects. The output I want should be something like this:
result = [{id:537,y:2}]
My code so far is like this
let result = [];
data.map((item) => {
if (item.name === "aBase") {
if (!result[item.id] === item.id) {
result[item.id] = item.id;
}
} else {
if (item.name === "aCourt") {
if (result[item.id] === item.aBaseId) {
if (!result[item.id].y || result[item.id].y > item.rating.y) {
result[item.id].y = item.rating.y;
}
}
}
}
});
How can I fix the code to produce the desired output?
aBaseelement always be first indata? If so, you can just useresult = [{ id: data[0]['id'], y: Math.min(...data.slice(1).map(e => e.rating.y)) }]