繁体   English   中英

如何将值数组和键数组推送到JavaScript对象?

[英]How to push an array of values and keys to a JavaScript Object?

我有一个非常大的Javascript对象,其中一个空白部分,我可以动态地向其添加数据。 出于这个问题的目的,我删除了对象的不必要部分。

这是我的目标:

var simplemaps_worldmap_mapdata = {
    locations:{ 

    }
}

这是我尝试将数据插入对象:

var mainObj = simplemaps_worldmap_mapdata;
var newObj = [];

newObj.push({
    name: 'newName',
    lat: 'newLat',
    lng: 'newLong',
    color: 'newColor',
    description: 'newDesc',
    url: 'newUrl',
    size: 'newSize',
    type: 'newType',
    opacity: 'newOpacity'
});

mainObj.locations.push(newObj);

在这里演示

为什么我不能动态地向我的对象添加数据?


编辑:

这是一个如何使用一个条目查看locations的示例:

locations:{ 
        0: { 
            name: 'newName',
            lat: 'newLat',
            lng: 'newLong',
            color: 'newColor',
            description: 'newDesc',
            url: 'newUrl',
            size: 'newSize',
            type: 'newType',
            opacity: 'newOpacity'
        },
    },

location属性正在初始化为object而不是数组。 尝试这个:

var simplemaps_worldmap_mapdata = {
    locations:[]
}

从您编辑的版本中,您还可以尝试以下方法:

Array.prototype.push.call(mainObj.locations, newObj);

如果可以更改您的JSON,最好将location属性更改为数组

JSON结构

var simplemaps_worldmap_mapdata = {
    locations: []
};

var mainObj = simplemaps_worldmap_mapdata;
var newObj = {
    name: 'newName',
    lat: 'newLat',
    lng: 'newLong',
    color: 'newColor',
    description: 'newDesc',
    url: 'newUrl',
    size: 'newSize',
    type: 'newType',
    opacity: 'newOpacity'
};

mainObj.locations.push(newObj);

的jsfiddle

更新

希望这可以解决您的问题

JSON结构

var simplemaps_worldmap_mapdata = {
    locations: {}
};

var mainObj = simplemaps_worldmap_mapdata;
var newObj = {
    name: 'newName',
    lat: 'newLat',
    lng: 'newLong',
    color: 'newColor',
    description: 'newDesc',
    url: 'newUrl',
    size: 'newSize',
    type: 'newType',
    opacity: 'newOpacity'
};

//Code to find no of keys
if (!Object.keys) {
    Object.keys = function (obj) {
        var keys = [],
            k;
        for (k in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, k)) {
                keys.push(k);
            }
        }
        return keys;
    };
}

var len = Object.keys(mainObj.locations).length;

mainObj.locations[len]= newObj;

结果

{
    "locations": {
        "0": {
            "name": "newName",
            "lat": "newLat",
            "lng": "newLong",
            "color": "newColor",
            "description": "newDesc",
            "url": "newUrl",
            "size": "newSize",
            "type": "newType",
            "opacity": "newOpacity"
        }
    }
}

的jsfiddle

对于多个locations

var simplemaps_worldmap_mapdata = {
    locations: []
}

var newObj = {
    name: 'newName',
    lat: 'newLat',
    lng: 'newLong',
    color: 'newColor',
    description: 'newDesc',
    url: 'newUrl',
    size: 'newSize',
    type: 'newType',
    opacity: 'newOpacity'
};

var mainObj = simplemaps_worldmap_mapdata;

mainObj.locations.push(newObj);

console.log(mainObj);

暂无
暂无

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

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