This is a follow up to Make 3 arrays correspond to each other with the first being the object name.
AFter I create my objects:
let objName = ["object1", "object2", "object3"];
let xyzArr = ["xyz1", "xyz2","xyz3"];
let theArr = [[], [], []];
objName.forEach((name, index) => {
window[name] = {
xyz: xyzArr[index],
arr: theArr[index]
};
});
I use getJSON and want to use those values to push into the array: arr[]
. I have multiple objects that have the arr[]
and I want to push values into all of them.
This is what I tried so far:
$.getJSON(json, result =>
{result.forEach((elem, i, array) =>
{object1.arr.push({x:elem.val1, y:elem.val2});
{object2.arr.push({x:elem.val1, y:elem.val2});
{object3.arr.push({x:elem.val1, y:elem.val2});
})
});
When I do this one by one, it works. No errors. Is there a way for me to push these same values into how many ever object[i]
I have?
I tried:
$.getJSON(json, result =>
{result.forEach((elem, i, array) =>
(for let j=0; j<=5; j++) {
{object[i].arr.push({x:elem.val1, y:elem.val2});
)
})
});
When I do this I get Cannot call method 'push' of undefined
.
Is there a way to do this without making my code long and bulky?