4

I have a very large Javascript Object, with a section that is blank, ready for me to dynamically add data to it. For the purpose of this question, I have removed unnecessary parts of my object.

This is my object:

var simplemaps_worldmap_mapdata = {
    locations:{ 

    }
}

This is my attempt to insert data into the object:

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

DEMO HERE

Why can't I dynamically add data to my object?


EDIT:

This is an example of how locations should look with one entry:

locations:{ 
        0: { 
            name: 'newName',
            lat: 'newLat',
            lng: 'newLong',
            color: 'newColor',
            description: 'newDesc',
            url: 'newUrl',
            size: 'newSize',
            type: 'newType',
            opacity: 'newOpacity'
        },
    },
1
  • 2
    push is a method in array, not in object Commented Jun 18, 2014 at 5:51

3 Answers 3

5

The location property is being initialized as object not as an array. Try this:

var simplemaps_worldmap_mapdata = {
    locations:[]
}

From your Edited Version you could also try something like:

Array.prototype.push.call(mainObj.locations, newObj);
Sign up to request clarification or add additional context in comments.

1 Comment

I believe that it needs to be an object though.. I will update my question now with a sample entry of what the data needs to look like upon adding.
2

If its possible to alter your JSON its better to change location attribute to an array

JSON structure

var simplemaps_worldmap_mapdata = {
    locations: []
};

Code

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

Update

Hope this fixes your issue

JSON structure

var simplemaps_worldmap_mapdata = {
    locations: {}
};

Code

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;

Result

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

JSFiddle

3 Comments

Unfortunately, I cannot change the location attribute to an array since I am using a plugin, and I cannot manipulate the object in any way.
Yes, that is completely right. Although, in my production code, this is within a loop, and I need to be able to add many, not just one.
The new code fixed it up perfectly, thanks a ton for your help!
1

For multiple 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);

2 Comments

Works very well. Although, I cannot change the location attribute to an array since I am using a plugin, and I cannot manipulate the object in any way.
Well, then you shouldn't use 'locations' :) If it's an object, you cannot have multiple entries there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.