0

Let's suppose I have an associative array like this one

var client1={
    "id":"1"
    "category":"Interiorism",
    "photo1":"img/ClientCorp/photoClient1.jpg",
    "photo2":"img/ClientCorp/photoClient2.jpg",
    "photo3":"img/ClientCorp/photoClient3.jpg",
    "photo4":"img/ClientCorp/photoClient4.jpg",
    };

var client2={
    .
    .
    .
    };



allClients=[client1, client2..., clientx];

I want to set up a function that pushs the photo keys in an empty array. The problem is that not all the clients have the same number of photos, so I am using 'for'. Here is the function I wrote

function photoKeys()
{
    var keyList=Object.keys(allClients[id]);
    var numKey=parseInt(listaKeys.length);

    var photoAlbum=[];  //here I want to put the photo URL's


    for (i=2; i<=numFotos; i++)
    {
        ????????????
    }

}

Here is the problem, how I can write the photo object from the client array whith the i var from the 'for' function?

I tried this but didn't work

for (i=2; i<=numFotos; i++)
        {
            photoAlbum.push(allClients[id].photo+'i');
        }
1
  • 2
    Honestly, if you have the option, it would probably be more reasonable to make a sub-array called "photos" that contains all the photos. That way you can just iterate through that without worrying about concatenating to some string. Commented Jul 27, 2015 at 16:12

3 Answers 3

1

Your current code would be parsed like this:

photoAlbum.push(allClients[id].photo + 'i');

It would try to evaluate allClients[id].photo and then append the string i. You need to access the property name using bracket notation instead of dot notation.

You also have the symbol and string part backward, photo is the string and i is your index variable.

photoAlbum.push(allClients[id]['photo' + i]);
Sign up to request clarification or add additional context in comments.

Comments

1

The big thing to understand is that client in your example isn't an array, it's an object.

var client1={
    "id":"1"
    "category":"Interiorism",
    "photo1":"img/ClientCorp/photoClient1.jpg",
    "photo2":"img/ClientCorp/photoClient2.jpg",
    "photo3":"img/ClientCorp/photoClient3.jpg",
    "photo4":"img/ClientCorp/photoClient4.jpg",
};

You can acquire an object's keys as an array using Object.keys(client1), or you can loop through all an object's keys using for...in syntax.

If you want to feed an arbitrary number (numFotos) of property values from your object into an array called photoAlbum, you can use the following syntax:

var i = 0;
for(var key in client1){
    photoAlbum.push(client1[key]);
    if(++i >= numFotos){
       break; // break out of the loop if i equals or exceeds numFotos
    }
}

1 Comment

Thank you! Very helpful answer!
0

First of all you should be accessing the photo paths like:

photoAlbum.push(allClients[id]['photo' + i]);

But i would really recommend you to change the format of your client object to something like this:

var client1 = {
    "id"       :"1"
    "category" :"Interiorism",
    "photos"   : [
        "img/ClientCorp/photoClient1.jpg",
        "img/ClientCorp/photoClient2.jpg",
        ...
    ]
};

Or this, if you need to store those "photo1", "photo2" ids:

var client2 = {
    "id"       :"1"
    "category" :"Interiorism",
    "photos"   : [
        {
            "id"   : "photo1",
            "path" :"img/ClientCorp/photoClient1.jpg"
        },
        ...
    ]
};

Then you can iterate them way easier like this:

for(var i = 0; i < allClients[id].photos.length; i++){
    photoAlbum.push(allClients[id].photos[i]);
    //or this for the second format:
    //photoAlbum.push(allClients[id].photos[i].path);
}

1 Comment

It worked perfectly, I changed the format like you suggested me and now is all much easier. Thank you!

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.