Destination Designs
Destination Designs

Reputation: 683

javascript object/array for loop to assigns keys

I have an object that looks like this:

var obj = {
    "cat" : [
        {val: 'res', string: "Residential & Single Family Homes"},
        {val: 'Dup', string: "Duplexes & Apartments"},
        {val: 'Con', string: "Condominiums"},
        {val: 'Lot', string: "Lots, Land & Farms"},
        {val: 'Com', string: "Commercial"},
        {val: 'Mob', string: "Mobile Homes"},
    ],
    "bdrms" : [
        {val: "1", string: "1 Bedroom"},
        {val: "2", string: "2 Bedrooms"},
        {val: "3", string: "3 Bedrooms"},
        {val: "4", string: "4 Bedrooms"},
        {val: "5", string: "5 Bedrooms"},
    ],
    "bthrms" : [
        {val: "1", string: "1 Bathroom"},
        {val: "1.1", string: "1 1/2 Bathrooms"},
        {val: "2", string: "2 Bathrooms"},
        {val: "2.1", string: "2 1/2 Bathrooms"},
        {val: "3", string: "3 Bathrooms"},
        {val: "3.1", string: "3 1/2 Bathrooms"},
        {val: "4", string: "4 Bathrooms"},
        {val: "4.1", string: "4 1/2 Bathrooms"},
        {val: "5", string: "5 Bathrooms"},
    ],
    "zip" : [

    ]
};

and what i would like to do is dynamically generate an array of zip codes inside "zip" using a for loop. what I have so far isnt working as I am getting a bunch of errors such as unexpected token ect...

im assuming that i am doing this totally wron, but this is what i am attempting to do:

"zip" : [
    for(i=43000; i<=45999;){
        {val: i, string: i},
        i++
    };
]

Upvotes: 1

Views: 62

Answers (6)

Nitesh Patel
Nitesh Patel

Reputation: 631

Try this as a simple readable solution:

"zip": getZipCodes()

function getZipCodes() {
    var codes = [];
    for(var i = 43000; i <= 45999; i++){
        codes.push({val: i, string: i});
    }

    return codes;
};

That way you do not modify the object after creation.

Upvotes: 0

dgavian
dgavian

Reputation: 250

You can't run your loop inside of the array literal. This will work:

   var obj = {
        "cat" : [
            {val: 'res', string: "Residential & Single Family Homes"},
            {val: 'Dup', string: "Duplexes & Apartments"},
            {val: 'Con', string: "Condominiums"},
            {val: 'Lot', string: "Lots, Land & Farms"},
            {val: 'Com', string: "Commercial"},
            {val: 'Mob', string: "Mobile Homes"},
        ],
        "bdrms" : [
            {val: "1", string: "1 Bedroom"},
            {val: "2", string: "2 Bedrooms"},
            {val: "3", string: "3 Bedrooms"},
            {val: "4", string: "4 Bedrooms"},
            {val: "5", string: "5 Bedrooms"},
        ],
        "bthrms" : [
            {val: "1", string: "1 Bathroom"},
            {val: "1.1", string: "1 1/2 Bathrooms"},
            {val: "2", string: "2 Bathrooms"},
            {val: "2.1", string: "2 1/2 Bathrooms"},
            {val: "3", string: "3 Bathrooms"},
            {val: "3.1", string: "3 1/2 Bathrooms"},
            {val: "4", string: "4 Bathrooms"},
            {val: "4.1", string: "4 1/2 Bathrooms"},
            {val: "5", string: "5 Bathrooms"},
        ],
        "zip" : [

        ]
    },
    zips = obj.zip,
    i,
    counter = 0,
    j;
    for(i=43000; i<=45999; i++){
        zips[counter] = { val: i, string: i };
        counter++
    };

Upvotes: -2

Sushanth --
Sushanth --

Reputation: 55750

"zip" : [ <---- Having a loop inside an array will throw an error for(i=43000; i<=45999;){ {val: i, string: i}, i++ }; ]

Supposed to look like

"zip": function () { //<--- Need to write up a function
    var arr = [];    // that will return an array instead
    for (var i = 43000; i <= 45999; i++) {
        var oo = {   // 
            val: i,
            string: i
        };
        arr.push(oo);
    };

    return arr;

}()

So basically it is a function that computes the array and returns the zip codes.

Upvotes: 1

Nathan Hornby
Nathan Hornby

Reputation: 1443

var zip = [];
for (var i = 43000; i <= 45999; i++) {
    zip.push({val: i, string: i});
}
console.log(zip);

Will output the zip array containing the objects you desire.

Edit: Beaten to the punch by Andy!

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074959

The simplest thing is just to do it after the object initializer:

for(i=43000; i<=45999;i++){
    obj.zip.push({val: i, string: i});
}

(Note that I moved the i++, it has no business being in the body of the loop.) (Also note that unless you have i declared somewhere, that was falling prey to The Horror of Implicit Globals.) (And for blocks don't have a ; after them. :-) )

If it were really important that it be part of the initializer (which is unlikely), you could use a temporary function you call immediately:

"zip" : function() {
    var a = [];
    for(var i=43000; i<=45999;i++){
        a.push({val: i, string: i});
    }
    return a;
}()

...but I wouldn't without a good reason. Though I'm not sure why not...

Upvotes: 4

Andy
Andy

Reputation: 63550

Just have an empty array zip: [] and then use a loop to push objects containing the values into it, making sure you var your loop variables:

for (var i = 43000; i <= 45999; i++) {
    obj.zip.push({ val: i, string: i });
};

DEMO

Upvotes: 2

Related Questions