00:00
00:00
Newgrounds Background Image Theme

StemlyAnarchy just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Pushing to an objects array | JS

1,124 Views | 4 Replies
New Topic Respond to this Topic

I've got this object, which I use to create dynamic variables:

$("polyline").each(function (index) {
        polylineObj["allPoints"+index] = document.getElementsByTagName("polyline")[index].getAttribute("points");
        polylineObj["allPointsArray"+index] = polylineObj["allPoints"+index].split(" ");
});

But also in this function each, I have this

polylineObj["yAxis"+index] = [];
polylineObj["allPointsArray"+index].forEach(function (value, index) {
      if (index % 2) {
          polylineObj["yAxis"+index].push(value);
     }
});

The polylineObj["yAxis"+index] works, but when I try to push the value - it doesn't work...

Not sure why. Any idea?

Response to Pushing to an objects array | JS 2018-02-14 05:51:16


Fixed

Changed it to

$("polyline").each(function (num) {

So that it didn't interfere with the forEach that used the index

Anything you think I can improve on? Let me know.

Also, I use pure js instead of jQuery when getting all the polyline's so that it acknowledges them in an array.

Response to Pushing to an objects array | JS 2018-02-19 20:32:20


I'm honestly still using pure jvs for everything. I've used jquery and I should probably use it more but I have always loved jvs so much.

That said, I think your problem is probably something to do with:

polylineObj["yAxis"+index].push(value);

You're pushing onto an element, not the array here. Push is only for adding elements to the end of an array. So it'd need to be polyineObj.push(value);

Unless there is some tricky advanced method for pushing to midpoints of an array. But I've never seen that.


∀x (∃e (e ∈ x ∧ ∀x ¬(x ∈ e)) ∨ ∃y ¬∃e (e ∈ x ∧ ¬∃z (z ∈ y ∧ z ∈ e ∧ ∀x ¬((x ∈ y ∧ x ∈ e) ∧ ¬(x = z)))))

Response to Pushing to an objects array | JS 2018-02-20 09:48:25


At 2/19/18 08:32 PM, sharpnova wrote: That said, I think your problem is probably something to do with:

polylineObj["yAxis"+index].push(value);

You're pushing onto an element, not the array here. Push is only for adding elements to the end of an array. So it'd need to be polyineObj.push(value);

Here's making the element an array a couple lines before that:

polylineObj["yAxis"+index] = [];

The problem was he was using index as a variable name for these two loops:

$("polyline").each(function (index) {});
polylineObj["allPointsArray"+index].forEach(function (value, index) {});

The latter of which, so far as I can tell, was nested in the former's callback and overwriting his expected index value.

At 2/19/18 08:32 PM, sharpnova wrote: Unless there is some tricky advanced method for pushing to midpoints of an array. But I've never seen that.

The splice() function can do that:

var foo = ["foo", "bar", "baz"];

foo.splice(2, 0, "hello");
console.log(foo); // ["foo", "bar", "hello", "baz"]

Response to Pushing to an objects array | JS 2018-02-20 12:11:46


At 2/20/18 09:48 AM, Diki wrote: Here's making the element an array a couple lines before that:

Ahh I see nm then.

The problem was he was using index as a variable name for these two loops:

Yep that must be it


∀x (∃e (e ∈ x ∧ ∀x ¬(x ∈ e)) ∨ ∃y ¬∃e (e ∈ x ∧ ¬∃z (z ∈ y ∧ z ∈ e ∧ ∀x ¬((x ∈ y ∧ x ∈ e) ∧ ¬(x = z)))))