1

Well, having a head scratching moment.

I'm trying to add additional data to my object after I request it from an external site (just for testing, I plan on adding random values)

I'll just cut to the chase:

For example purposes, my test.json file looks like this:

[["month",[-150,100,0.7]]]

And after acquiring the JSON file, I need it to look like this:

[["month",[-150,100,0.7,24,24,0.5]]]

Request:

    xhr = new XMLHttpRequest();
    xhr.open('GET', '/test.json', true);
    xhr.onreadystatechange = function(e) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          var data = JSON.parse(xhr.responseText);

          // Trying to add this as an additional array
          data[0].push([24,24,0.5]);

          window.data = data;
          for (i=0;i<data.length;i++) {
            globe.addData(data[i][1], {format: 'magnitude', name: data[i][0], animated: true});
          }
          globe.createPoints();
          settime(globe,0)();
          globe.animate();
          document.body.style.backgroundImage = 'none'; // remove loading
        }
      }
    };
    xhr.send(null);

Here's a screenshot of the hierarchy that I see with dev tools:

Adding it as a new array, making an even deeper multi-dimensional array instead of just adding to the existing one.

It's adding the data deeper into the model... I'm just a bit lost how to structure this.

(Making a project with the WebGL - Globe Google Project, FYI)

Any easier way if I have a data-set than just doing...?

data[0][1].push(24);
data[0][1].push(24);
data[0][1].push(0.5);
5
  • .push() can take multiple parameters, so you could do data[0][1].push(24, 24, 0.5);. Commented Mar 21, 2014 at 4:34
  • So I'm just a goofball and added it AS an array like a tard? lol Commented Mar 21, 2014 at 4:38
  • 1
    Don't beat yourself up too bad...we've all made these mistakes. But if the new values you want to append are stored as an array in a variable, then @ArtyomNeustroev's answer below looks like a good solution. Commented Mar 21, 2014 at 4:40
  • Thanks :-) View Live Here with added Data Commented Mar 21, 2014 at 4:40
  • Essentially this will be a synchronous function. I'm going to cache the most recent 10 requests... now I need to figure out how to remove the values after the 11th request lol. Commented Mar 21, 2014 at 4:43

1 Answer 1

2

Use Array.prototype.concat:

 var yourArray = [24,24,0.5];
 data[0][1] = data[0][1].concat(yourArray);

Or use apply with push:

data[0][1].push.apply(data[0][1], yourArray);
Sign up to request clarification or add additional context in comments.

Comments

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.