动态添加变量名称值对到 JSON 对象

发布于 2024-09-30 00:20:42 字数 391 浏览 8 评论 0原文

我有一个充满 ips 的 json 对象,就像

var ips = {}

我然后将 ip 对象添加到这个对象一样,然后

ips[ipID] = {}

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

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

我的问题是,如何添加这些名称值对/ tmp 到我的 ipID 对象,这样我的结果就像

ipID = { name : value, anotherName : anotherValue }

I have a json object full of ips like

var ips = {}

I then add ip objects to this object like so

ips[ipID] = {}

I then need to add dynamic/variable name value pairs to each ip so I am using code like this

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

My question is, how can I add these name value pairs/ tmp to my ipID objects so that my outcome turns out like

ipID = { name : value, anotherName : anotherValue }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明媚殇 2024-10-07 00:20:43

在 JavaScript 中。

    var myObject = { "name" : "john" };
    // { "name" : "john" };
    myObject.gender = "male";
    // { "name" : "john", "gender":"male"};

in Javascript.

    var myObject = { "name" : "john" };
    // { "name" : "john" };
    myObject.gender = "male";
    // { "name" : "john", "gender":"male"};
尹雨沫 2024-10-07 00:20:43

如果我对您最初的 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"
  }
}

if my understanding of your initial JSON is correct, either of these solutions might help you loop through all ip ids & assign each one, a new object.

// 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'});
});

To confirm:

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

The above statement spits:

{
  "ipId1": {
    "name":"value",
    "anotherName":"another value"
  },
  "ipId2": {
    "name":"value",
    "anotherName":"another value"
  }
}
水波映月 2024-10-07 00:20:43

从其他答案提出的内容来看,我相信这可能会有所帮助:

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}

From what the other answers have proposed, I believe this might help:

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

However, this is not tested, just an assumption based on the constant repetition of:

object["string"] = value;
//object = {string: value}
感悟人生的甜 2024-10-07 00:20:43

您可以使用 Lodash _.assign 功能。

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

You can achieve this using Lodash _.assign function.

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

笑红尘 2024-10-07 00:20:42

那不是 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] 时,访问变量时变量中的字符串决定将访问哪个属性。

That's not JSON. It's just Javascript objects, and has nothing at all to do with JSON.

You can use brackets to set the properties dynamically. Example:

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

This gives exactly the same as creating the object with an object literal like this:

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

If you have already added the object to the ips collection, you use one pair of brackets to access the object in the collection, and another pair to access the propery in the object:

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

Notice similarity with the code above, but that you are just using ips[ipId] instead of obj.

You can also get a reference to the object back from the collection, and use that to access the object while it remains in the collection:

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

You can use string variables to specify the names of the properties:

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

It's value of the variable (the string) that identifies the property, so while you use obj[name] for both properties in the code above, it's the string in the variable at the moment that you access it that determines what property will be accessed.

于我来说 2024-10-07 00:20:42

ECMAScript 6 有更好的方法。

您可以在对象属性定义中使用计算属性名称,例如:

var name1 = 'John'; 
var value1 = '42'; 
var name2 = 'Sarah'; 
var value2 = '35';

var ipID = { 
             [name1] : value1, 
             [name2] : value2 
           }

这相当于以下内容,其中您有属性名称的变量。

var ipID = { 
             John: '42', 
             Sarah: '35' 
           }

With ECMAScript 6 there is a better way.

You can use computed property names in object property definitions, for example:

var name1 = 'John'; 
var value1 = '42'; 
var name2 = 'Sarah'; 
var value2 = '35';

var ipID = { 
             [name1] : value1, 
             [name2] : value2 
           }

This is equivalent to the following, where you have variables for the property names.

var ipID = { 
             John: '42', 
             Sarah: '35' 
           }
哭泣的笑容 2024-10-07 00:20:42

使用 javascript 对象时,您也可以只使用“点符号”来添加项目,(JSLint 更喜欢哪个

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

when using javascript objects, you can also just use "dot notation" to add an item, (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"}

Here is a screenshot from testing in the Chrome console

screenshot

云仙小弟 2024-10-07 00:20:42

我假设“ips”中的每个条目都可以有多个名称值对 - 所以它是嵌套的。您可以这样实现此数据结构:

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;
}

I'm assuming each entry in "ips" can have multiple name value pairs - so it's nested. You can achieve this data structure as such:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文