动态添加变量名称值对到 JSON 对象
我有一个充满 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 JavaScript 中。
in Javascript.
如果我对您最初的 JSON 的理解是正确的,那么这些解决方案中的任何一个都可以帮助您循环遍历所有 ip id 和 ip id。为每个对象分配一个新对象。
确认:
以上说法吐槽:
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.
To confirm:
The above statement spits:
从其他答案提出的内容来看,我相信这可能会有所帮助:
但是,这没有经过测试,只是基于不断重复的假设:
From what the other answers have proposed, I believe this might help:
However, this is not tested, just an assumption based on the constant repetition of:
您可以使用 Lodash
_.assign
功能。You can achieve this using Lodash
_.assign
function.那不是 JSON。它只是 Javascript 对象,与 JSON 完全无关。
您可以使用括号动态设置属性。示例:
这与使用对象字面量创建对象完全相同:
如果您已将对象添加到 ips 集合中,则可以使用一对括号来访问集合中的对象,和另一对来访问对象中的属性:
请注意与上面代码的相似之处,但您只是使用
ips[ipId]
而不是obj
。您还可以从集合中获取对对象的引用,并在对象保留在集合中时使用它来访问该对象:
您可以使用字符串变量来指定属性的名称:
它是变量的值(字符串)标识属性,因此当您对上面代码中的两个属性使用 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:
This gives exactly the same as creating the object with an object literal like this:
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:Notice similarity with the code above, but that you are just using
ips[ipId]
instead ofobj
.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:
You can use string variables to specify the names of the properties:
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.ECMAScript 6 有更好的方法。
您可以在对象属性定义中使用计算属性名称,例如:
这相当于以下内容,其中您有属性名称的变量。
With ECMAScript 6 there is a better way.
You can use computed property names in object property definitions, for example:
This is equivalent to the following, where you have variables for the property names.
使用 javascript 对象时,您也可以只使用“点符号”来添加项目,(JSLint 更喜欢哪个)
这是在 Chrome 控制台中测试的屏幕截图
when using javascript objects, you can also just use "dot notation" to add an item, (which JSLint prefers)
Here is a screenshot from testing in the Chrome console
我假设“ips”中的每个条目都可以有多个名称值对 - 所以它是嵌套的。您可以这样实现此数据结构:
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: