代码之家  ›  专栏  ›  技术社区  ›  Mike

动态向JSON对象添加变量名-值对

  •  56
  • Mike  · 技术社区  · 14 年前

    var ips = {}
    

    然后我将ip对象添加到这个对象,如下所示

    ips[ipID] = {}
    

    然后我需要向每个ip添加动态/变量名-值对,所以我使用这样的代码

    var name; var value; var temp = {};
    tmp[name] = value
    

    我的问题是,如何将这些名称-值对/tmp添加到我的ipID对象中,以使我的结果如下

    ipID = { name : value, anotherName : anotherValue }
    
    8 回复  |  直到 7 年前
        1
  •  173
  •   Guffa    14 年前

    那不是JSON。它只是Javascript对象,与JSON毫无关系。

    可以使用方括号动态设置属性。例子:

    var obj = {};
    obj['name'] = value;
    obj['anotherName'] = anotherValue;
    

    var obj = { name : value, anotherName : anotherValue };
    

    如果已将对象添加到 ips 集合中,使用一对方括号访问集合中的对象,使用另一对方括号访问对象中的属性:

    ips[ipId] = {};
    ips[ipId]['name'] = value;
    ips[ipId]['anotherName'] = anotherValue;
    

    ips[ipId] 而不是 obj .

    ips[ipId] = {};
    var obj = ips[ipId];
    obj['name'] = value;
    obj['anotherName'] = anotherValue;
    

    可以使用字符串变量指定属性的名称:

    var name = 'name';
    obj[name] = value;
    name = 'anotherName';
    obj[name] = anotherValue;
    

    它是标识属性的变量(字符串)的值,因此在使用 obj[name] 对于上面代码中的两个属性,决定要访问哪个属性的是访问变量时变量中的字符串。

        2
  •  22
  •   Dylan Hogg    6 年前

    你可以用 computed property names 在对象属性定义中,例如:

    var name1 = 'John'; 
    var value1 = '42'; 
    var name2 = 'Sarah'; 
    var value2 = '35';
    
    var ipID = { 
                 [name1] : value1, 
                 [name2] : value2 
               }
    

    var ipID = { 
                 John: '42', 
                 Sarah: '35' 
               }
    
        3
  •  7
  •   Max    8 年前

    当使用javascript对象时,也可以使用“点符号”来添加一个项( which JSLint prefers )

    var myArray = { name : "john" };
    //will initiate a key-value array with one item "name" and the value "john"
    myArray.lastName = "smith";
    //will add a key named lastName with the value "smith"
    //Object {name: "john", lastName: "smith"}
    

    这是Chrome控制台测试的截图

    screenshot

        4
  •  5
  •   orangepips 111111    14 年前

    var ips = {}
    
    function addIpId(ipID, name, value) {
        if (!ips[ipID]) ip[ipID] = {};
        var entries = ip[ipID];
        // you could add a check to ensure the name-value par's not already defined here
        var entries[name] = value;
    }
    
        5
  •  3
  •   Atif Hussain    8 年前

    在Javascript中。

        var myObject = { "name" : "john" };
        // { "name" : "john" };
        myObject.gender = "male";
        // { "name" : "john", "gender":"male"};
    
        6
  •  1
  •   user4953229    9 年前

    根据其他答案的建议,我认为这可能有助于:

    var object = ips[ipId];
    var name = "Joe";
    var anothername = "Fred";
    var value = "Thingy";
    var anothervalue = "Fingy";
    object[name] = value;
    object[anothername] = anothervalue;
    

    object["string"] = value;
    //object = {string: value}
    
        7
  •  1
  •   Hamzeen Hameem    6 年前

    如果我对初始JSON的理解是正确的,那么这些解决方案中的任何一个都可以帮助您遍历所有ip id,并为每个ip id分配一个新对象。

    // initial JSON
    var ips = {ipId1: {}, ipId2: {}};
    
    // Solution1
    Object.keys(ips).forEach(function(key) {
      ips[key] = {name: 'value', anotherName: 'another value'};
    });
    
    // Solution 2
    Object.keys(ips).forEach(function(key) {
      Object.assign(ips[key],{name: 'value', anotherName: 'another value'});
    });
    

    确认:

    console.log(JSON.stringify(ips, null, 2));
    

    上面的说法是:

    {
      "ipId1": {
        "name":"value",
        "anotherName":"another value"
      },
      "ipId2": {
        "name":"value",
        "anotherName":"another value"
      }
    }
    
        8
  •  0
  •   Penny Liu    6 年前

    矿脉 _.assign

    var ipID = {};
    _.assign(ipID, {'name': "value"}, {'anotherName': "anotherValue"});
    console.log(ipID);
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>