2

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?

3
  • I'm surprised that object1.data.push works when data isn't defined on object1 ? Commented Aug 20, 2019 at 10:53
  • 1
    That was my bad. Let me fix the code. I forgot to fix that when I typed it out here Commented Aug 20, 2019 at 10:55
  • Make a jsFiddle to demonstrate the problem. Would make it a thousand times easier to debug than the already buggy js you’re adapting from your real code. Commented Aug 20, 2019 at 11:00

2 Answers 2

2

Instead of dynamically creating variables using window, you can use an array to store a collection of your objects like so:

let xyzArr = ["xyz1", "xyz2", "xyz3"];
let theArr = [[], [], []];
let objects = [];

objName.forEach((name, index) => {
  objects.push({
    xyz: xyzArr[index],
    arr: theArr[index]
  });
});

And then when adding data to each object, you can loop through it using a .forEach() on your objects array like so:

$.getJSON(json, result => {
  result.forEach(elem => {
    objects.forEach(obj => {
      obj.arr.push({
        x: elem.val1,
        y: elem.val2
      });
    });
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to use an array of objects. Is there anyways to use forEach with out the array?
@noob hey, is there a particular reason why you can't use arrays? Arrays are the perfect thing to use for cases like these
2

object[i] refers to an array named object which obviously doesn't exist.

Try this oneliner instead:

$.getJSON(json, result => result.forEach((elem, i, array) => window['object' + (i + 1)].data.push({ x: elem.val1, y: elem.val2 })));

4 Comments

I am getting TypeError: Cannot read property 'data' of undefined error
then your result has a greater length than the existing objects?
pretty sure you would need to use window['object' + i+1]
I think the value of i could exceed 3 though. You'd most likely need an inner for-loop to loop through all the object variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.