I have multiple functions that calculate values in an array of objects. I am trying to run the functions through all 7,200 objects in my array, and in turn add the return of those functions as a new Key-Value pair in each object. Below is a sample with a 5 object array instead of a 7200 object array.
I am new to JavaScript and am not sure how solve this. I can access the "Status" of each object using the const currentDay = myData.at(-1), console log the result and manually insert it as a new Key-Value Pair in the last object, change currentDay to (-2) etc. As I have 7000 objects to do this with, I am looking for a way to not do this manually. I want my resulting new Array to look like this:
const myData2 = [
{
Date: "1990-02-09",
End: "500",
PreviousEnd: "480",
NextEnd: "515",
Status: ["Positive 2 Day", "Next Day Up"],
},
{
Date: "1990-02-10",
End: "515",
PreviousEnd: "500",
NextEnd: "505",
Status: ["Positive 1 Day", "Next Day Down"],
},
{
Date: "1990-02-11",
End: "505",
PreviousEnd: "515",
NextEnd: "530",
Status: ["Negative", "Next Day Up"],
},
{
Date: "1990-02-12",
End: "530",
PreviousEnd: "525",
NextEnd: "555",
Status: ["Positive 2 Day", "Next Day Up"],
},
{
Date: "1990-02-13",
End: "555",
PreviousEnd: "530",
NextEnd: "570",
Status: ["Positive 2 Day", "Next Day Up"],
},
];
This is my current code.
const myData = [
{
Date: "1990-02-09",
End: "500",
PreviousEnd: "480",
NextEnd: "515",
},
{
Date: "1990-02-10",
End: "515",
PreviousEnd: "500",
NextEnd: "505",
},
{
Date: "1990-02-11",
End: "505",
PreviousEnd: "515",
NextEnd: "530",
},
{
Date: "1990-02-12",
End: "530",
PreviousEnd: "525",
NextEnd: "555",
},
{
Date: "1990-02-13",
End: "555",
PreviousEnd: "530",
NextEnd: "570",
},
];
const currentDay = myData.at(-1);
const endUpDown = (function () {
if (
currentDay.End > currentDay.PreviousEnd &&
currentDay.End > currentDay.NextEnd
) {
return "Positive 1 Day";
} else if (
currentDay.End > currentDay.PreviousEnd &&
currentDay.End < currentDay.NextEnd
) {
return "Positive 2 Day";
} else {
return "Negative";
}
})();
const nextDayUp = (function () {
if (currentDay.End < currentDay.NextEnd) {
return "Next Day Up";
} else {
return "Next Day Down";
}
})();
const currentStatus = [endUpDown, nextDayUp];
console.log(currentStatus);
CodePudding user response:
You have all the parts, you just need to reshuffle them to make them callable from within a map()
call. All I've done is make your IIFEs into named functions with a parameter currentDay
which expects an object rather than calling them on a global currentDay
as in your example.
The map()
call simply clones the passed object using spread syntax and then adds the status
property as a tuple of your two function calls.
const myData = [{ Date: '1990-02-09', End: '500', PreviousEnd: '480', NextEnd: '515', }, { Date: '1990-02-10', End: '515', PreviousEnd: '500', NextEnd: '505', }, { Date: '1990-02-11', End: '505', PreviousEnd: '515', NextEnd: '530', }, { Date: '1990-02-12', End: '530', PreviousEnd: '525', NextEnd: '555', }, { Date: '1990-02-13', End: '555', PreviousEnd: '530', NextEnd: '570', },];
function endUpDown(currentDay) {
if (
currentDay.End > currentDay.PreviousEnd &&
currentDay.End > currentDay.NextEnd
) {
return 'Positive 1 Day';
} else if (
currentDay.End > currentDay.PreviousEnd &&
currentDay.End < currentDay.NextEnd
) {
return 'Positive 2 Day';
} else {
return 'Negative';
}
}
function nextDayUp(currentDay) {
if (currentDay.End < currentDay.NextEnd) {
return 'Next Day Up';
} else {
return 'Next Day Down';
}
}
const result = myData.map((currentDay) => ({
...currentDay,
status: [endUpDown(currentDay), nextDayUp(currentDay)],
}));
console.log(result);