简体   繁体   English

如何添加添加到javascript对象

[英]How do I add add to a javascript object

How to I add to userInfo in my code. 如何在代码中添加到userInfo。 I am new to javascript. 我是javascript新手。 Not sure if I am asking properly 不知道我问的是否正确

Here is the data 这是数据

    var userInfo = {
        'a': {
            id: 1,
            vehicle: 'car'
        },
        'b': {
            id: 2,
            vehicle: 'boat'
        },
        'c': {
            id: 3,
            vehicle: 'plane'
        },
        'd': {
            id: 4,
            vehicle: 'plane'
        },
        'e': {
            id: 5,
            vehicle: 'bike'
    }

To add a new property, simply use 要添加新属性,只需使用

Object.Property = //whatever you want here

If there is no property existing, it will create a new one, and if there is already a property then it will overwrite it. 如果不存在任何属性,它将创建一个新属性,如果已经存在一个属性,则它将覆盖它。

So for your code, you would want 所以对于您的代码,您将需要

userInfo.f = {id: 6, vehicle: 'bus'}
userInfo.f = {id: 6, vehicle: 'train'};

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

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