I have a function that takes in an object as a prop and needs to add arrays to it using a for loop. My problem is that it only works if a single array is added when it is called. If more than one array is to be added, I receive the error linkLineItems.push is not a function, but I thought .push could be used to add arrays to objects.
Here is the function:
function PrepareSuccessorActivityLinkData(data, existingLinks, linkSetter) {
for (let [key, value] of Object.entries(data)) {
let linkLineItems;
let linkLineItem;
if (data.activitiesafter[0] != "DEFAULT") {
for (var i = 0; i < data.activitiesafter.length; i++) {
linkLineItem = {
source: data.itemname,
target: data.activitiesafter[i],
type: "activity-activity-after"
};
if (!linkLineItems) {
linkLineItems = linkLineItem;
} else {
linkLineItems.push(linkLineItem);
}
}
} else {
continue;
}
return linkSetter(linkData => [...existingLinks, linkLineItems]);
}
}
Any help on how to add multiple arrays to an object?
Edit #1: Sample data for the existingLinks or object that I'm trying to add items to
var linksData = [
{"source": "Do Something", "target": "My Document", "type": "Activity Output"},
{"source": "My Document", "target": "Operator", "type": "Object Responsible"},
{"source": "Operator", "target": "Do Something", "type": "Role Activity"}
];
Edit #2: Sample data that's being passed into the function as data
[{
itemname: "Hello World",
itemtype: "activity",
activitiesafter: ["Do Stuff", "Do Other Stuff"]
}]
linkLineItems.something = []pushcan only be used on Arrays. It would be helpful if you add some example input/output data. It's a little difficult to tell exactly what your desired behavior is hereactivitiesafterforloop.databeing passed into the function