简体   繁体   English

如何在JavaScript对象上执行“推”操作?

[英]How do you perform `push` on a JavaScript object?

What I am trying to achieve is use push() on a JavaScript object , that looks like so: 我想要实现的是在JavaScript对象上使用push() ,如下所示:

var apps = {
    app1:
    [
        "ci-extension",
        "Unnamed",
        "<h1>Hello world!</h1>"
    ],
    app2:
    [
        "ci-extension",
        "Another one!",
        "Cool!"
    ]
}

The goal is to add a third key " demo " to the object and then push another three lines to it. 目的是向对象添加第三个键“ demo ”,然后再向其推三行。 So after all the pushing, the object should look like this: 因此,在所有推送之后,对象应如下所示:

var apps = {
    app1:
    [
        "ci-extension",
        "Unnamed",
        "<h1>Hello world!</h1>"
    ],
    app2:
    [
        "ci-extension",
        "Another one!",
        "Cool!"
    ],
    demo:
    [
        "ci-google",
        "My demo app!",
        "Some content"
    ]
}

What I tried? 我尝试了什么?

I tried the following piece of code Object.keys(apps).push("demo"); 我尝试了以下代码Object.keys(apps).push("demo"); (if this makes any sense). (如果有任何意义)。 As an output, I just got 3 and the key "demo" was not added to the object. 作为输出,我只有3个 ,并且键“ demo”没有添加到对象中。
And then to add items into demo I tried using this tiny piece of code Object.keys(apps)["demo"].push("blah blah blah..."); 然后将项目添加到演示中,我尝试使用这段小代码Object.keys(apps)["demo"].push("blah blah blah..."); , but it totally did not work. ,但完全无效。


My brain and thoughts got obfuscated. 我的大脑和思想变得模糊。 Any help would be highly appreciated and very welcome. 任何帮助将不胜感激,非常欢迎。 Thanks in advance! 提前致谢!

You don't need to push() here, that's for adding to an exising array. 您无需在此处push() ,这是为了添加到现有数组。 You just want to add a key. 您只想添加一个密钥。 The easiest way is like this: 最简单的方法是这样的:

apps.demo = [ "ci-google", "My demo app!", "Some content" ];

Or, equivalently 或者,等效地

apps["demo"] = [ "ci-google", "My demo app!", "Some content" ];

The reason your code doesn't work is because Object.keys( apps ) creates you a new array containing all the keys in the apps object, but doing stuff to that array doesn't do anything to the actual apps object - you're just editing your new array. 您的代码不起作用的原因是因为Object.keys( apps )创建了一个新数组,其中包含apps对象中的所有键,但是对该数组执行操作对实际的apps对象没有任何作用-只是编辑您的新数组。 What you need to do is change the apps object directly (as I've shown above). 您需要做的是直接更改apps对象(如上所示)。

Note: Duncan's answer has the lowest learning curve. 注意:邓肯的答案具有最低的学习曲线。

With that said - especially since you are already using the .keys() method on the built-in Object type - two options you can look into are: 这么说(尤其是因为您已经在内置Object类型上使用了.keys()方法),您可以查看以下两个选项:

  1. Object.assign() Object.assign()
  2. Object.create() Object.create()

Both methods will return new objects with properties included that you pass as the second argument to the function. 这两种方法都将返回包含属性的新对象,这些属性将作为第二个参数传递给函数。

Object.assign(demo, {new_property}) will return an object with demo's own properties - thereby, neglecting the prototype chain. Object.assign(demo, {new_property})将返回具有demo 自身属性的对象-从而忽略了原型链。

Object.create(demo, {new_property}) will return an object that is prototype linked to demo. Object.create(demo, {new_property})将返回链接到演示的原型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM