我需要将数据添加到数组中

时间:2013-11-05 13:18:44

标签: javascript json

这就是我所拥有的......

格式良好的JSON数组

[
    {"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"}
]

现在我想为这个数组的每个部分添加两个新元素......

我正在使用:

    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;

    }

所以新数组看起来像这样:

[
    {"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"}
]

怎么做的???或甚至可以完成?

2 个答案:

答案 0 :(得分:1)

您要做的就是为存储在数组中的每个对象添加一些属性。这非常简单:遍历数组,将所需的属性添加到当前元素。

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
}

注意:上面的代码假设您将字符串作为参数传递给addPrefix函数,因为这就是JSON的含义。它是一种用于在语言之间传递信息的数据交换格式,并作为字符串传输。您在问题中发布的内容实际上是一个JavaScript数组。没有“JSON数组”这样的东西;你要么有一个数组,要么你有一个代表一个的JSON字符串。说“JSON数组”应该意味着后者(虽然你根本不应该使用它),但人们经常用它来表示前者。

答案 1 :(得分:0)

修改数组结构的最简单方法是使用 - map。我更喜欢下划线,因为某些浏览器仍然不支持此功能。

您可以轻松地执行此操作:

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