Peter The Angular Dude
Peter The Angular Dude

Reputation: 1188

I need to add data into an Array

Here's what I have...

The well formed JSON array

[
    {"id":"1","networkType":1,"controllerIp":"10.255.135.22","redirectType":0,"networkCost":1351,"networkWeight":64888,"notes":"This is a network for network 1","name":"n1"},
    {"id":"4","networkType":1,"controllerIp":"10.255.150.24","redirectType":0,"networkCost":1344,"networkWeight":745,"notes":"This is a network for network 4","name":"n4"},
    {"id":"3","networkType":2,"controllerIp":"10.255.150.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 3","name":"n3"},
    {"id":"2","networkType":3,"controllerIp":"10.255.654.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 2","name":"n2"}
]

Now I want to tack on two new elements to each part of this array...

I'm using:

    function addPrefix(rawJSON){

        var orgStr = rawJSON;

            //Loop through all the groups of .... networks, sites, resources, components
            $.each(myData1,function(key,val){
                /*
                    key would be key1,key2,key3
                    innerjson would be the name and value **
                */

                //Alerts and logging of the variable. VAL is the entire group {...}
                console.log(val); //should show you the value   
                //Now append the following to the groups...
                //"data" : val.name,
                //"metadata" : {"id" : val.id},


                alert(val.name); //Should say n1,n2,n3...nn
            });

            return orgStr;

    }

So the new array would look like this:

[
    {"data" : "1", "metadata" : {"id" : "n1"}, "id":"1","networkType":1,"controllerIp":"10.255.135.22","redirectType":0,"networkCost":1351,"networkWeight":64888,"notes":"This is a network for network 1","name":"n1"},
    {"data" : "4", "metadata" : {"id" : "n4"}, "id":"4","networkType":1,"controllerIp":"10.255.150.24","redirectType":0,"networkCost":1344,"networkWeight":745,"notes":"This is a network for network 4","name":"n4"},
    {"data" : "2", "metadata" : {"id" : "n2"}, "id":"3","networkType":2,"controllerIp":"10.255.150.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 3","name":"n3"},
    {"data" : "3", "metadata" : {"id" : "n3"}, "id":"2","networkType":3,"controllerIp":"10.255.654.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 2","name":"n2"}
]

How is that done??? or can it even be done?

Upvotes: 0

Views: 78

Answers (2)

lukas.pukenis
lukas.pukenis

Reputation: 13597

Easiest way to modify array structures is to use - map. I prefer underscore as some browser still do not have support for this function.

You can easily do this:

data = _(data).map(function(value, index) { 
  value.push(some_more_data);
  return value; 
});

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

All you want to do is add a few properties to each object stored in your array. That's pretty trivial: iterate through the array, adding the required properties to the current element.

function addPrefix(rawJSON) {
    var yourArray = JSON.parse(rawJSON); // parse the JSON string to an actual array
    $.each(yourArray, function(index, element) {
        element.data = element.id;
        element.metadata = {id : element.name};
    });
    return JSON.stringify(yourArray); // converts the array, with the new properties, back to a JSON string
}

NOTE: The code above assumes you're passing a string as the argument to the addPrefix function, because that's what JSON is. It's a data interchange format for passing information between languages, and is transported as a string. What you've posted in the question is actually a JavaScript array. There's no such thing as a "JSON array"; you either have an array or you have a JSON string that represents one. Saying "JSON array" should mean the latter (though you shouldn't use it at all), but people frequently use it to mean the former.

Upvotes: 1

Related Questions