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"
    }
];
5
  • Have you attempted to solve the solution on your own at all? Where are you getting stuck? Commented Apr 2, 2013 at 23:05
  • 1
    jsfiddle.net/DerekL/CCfWK Commented Apr 2, 2013 at 23:06
  • Do you want a shallow copy (i.e. if listings[0].address is changed you see the change in listings[2].address, too), or a deep copy (i.e. all elements are unrelated). Commented Apr 2, 2013 at 23:07
  • 1
    @Derek朕會功夫 Array.push can handle multiple arguments as well: jsfiddle.net/CCfWK/2 Commented Apr 2, 2013 at 23:08
  • Copy as in cloning, i.e. creating new objects? Commented Apr 2, 2013 at 23:11

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This just references the same objects though, it doesn't copy them.
Thanks for the response, how does % work? Where can I find info on this, just want to know exactly how this works, thanks
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].
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;
}

Comments

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.

2 Comments

The list-length will be matched at least three times as undefined. Not very clean :)
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.
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];
    }
}

Comments

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.