0

I would like to copy the 3 objects in my array until there is a total of 50. How do I go about doing this?

var listings = [
    { 
        address: "123 41st St",
        bedrooms: 2,
        bathrooms: 2,
        image: "img/1bdrm_a.jpg"
    },
    { 
        address: "234 52nd St",
        bedrooms: 1,
        bathrooms: 1,
        image: "img/1bdrm_b.jpg"
    },
    { 
        address: "345 63rd St",
        bedrooms: 3,
        bathrooms: 2,
        image: "img/1bdrm_c.jpg"
    }
];
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
user1644644
  • 123
  • 1
  • 1
  • 4

4 Answers4

4

This loop should do the trick:

for (var i = 3; i < 50; i++) {
  listings[i] = listings[i%3];
}

The magic is in the modular operator (%). i can increment to whatever you need it to but i%3 will keep returning 0, 1, and 2 in that order.

David Starkey
  • 1,840
  • 3
  • 32
  • 48
  • This just references the same objects though, it doesn't copy them. – Daniel Imms Apr 02 '13 at 23:12
  • Thanks for the response, how does % work? Where can I find info on this, just want to know exactly how this works, thanks – user1644644 Apr 02 '13 at 23:25
  • Daniel: It is simple enough to expand into copying. Just do `listings[i].field=listings[i%3].field;` for each of your fields. This can be made simpler by making a loop through the fields of `listings[i%3]`. – David Starkey Apr 03 '13 at 18:52
2

You could make a deep copy (clone) of each item using a method like this. We can avoid using % for this solution as it will serve no purpose ultimately.

jsFiddle

var i = 0;
while (listings.length < 50) {
    listings[listings.length] = clone(listings[i++]);
}
console.log(listings);


//https://stackoverflow.com/a/122190/1156119
function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = obj.constructor(); // changed

    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
}
Community
  • 1
  • 1
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
0
while(listings.length <= 50){
    listings[listings.length] = listings[listings.length - 3]
}

I prefer the while loop to the for loop for something like this.

ckersch
  • 7,507
  • 2
  • 37
  • 44
  • The list-length will be matched at least three times as `undefined`. Not very clean :) –  Apr 02 '13 at 23:11
  • Starting with a list length of 3, as in the original post, when will the list length come back as undefined? Though a modulo operator would be a bit nicer of a way of pulling the right item from the beginning of the list. – ckersch Apr 02 '13 at 23:13
0
var listings = [
    { 
        address: "123 41st St",
        bedrooms: 2,
        bathrooms: 2,
        image: "img/1bdrm_a.jpg"
    },
    { 
        address: "234 52nd St",
        bedrooms: 1,
        bathrooms: 1,
        image: "img/1bdrm_b.jpg"
    },
    { 
        address: "345 63rd St",
        bedrooms: 3,
        bathrooms: 2,
        image: "img/1bdrm_c.jpg"
    }
];
var len=listings.length;
for(var i=0;i<listings.length;i++){
    for(var j=i;j<50;j+=len){
        listings[j]=listings[i];
    }
}
Tom
  • 4,422
  • 3
  • 24
  • 36