0

I'm trying to parse JSON string with few arrays and some key:value pairs. I'm now able to get values from one array but I want to get values from the other array too. The arrays have same keys but I can't find out how to get data from the second array.

My jQuery script looks like this

$("a").click(function () {
    var page = $(this).text();
    console.log(page);
    $.post("/services/gallery/getimage", {"path[]":[<%=path %>], "page": page}, function(data){
        var length = data.previews.length;
        console.log(length);
        var html = "";
        for(var i = 0; i < length; i++){
            $.each(data.previews[i], function (index, value) {
                html+= "<a href=\""+value+"\"><img src=\""+value+"\" alt=\""+index+"\" /></a>";
                console.log("index: "+index);
                console.log("value: "+value);
                console.log(data.heights[i].index);
            });
        }

        $("#gallery").html(html);
    });
});

I've also tried some modifications of this console.log(data.heights[i].index); but still no usable result.

The console output is

1
15

index: index1
value: /some/path
undefined

index: index2
value: /some/paht
undefined 

and the JSON string looks like this

{
    "previews": [
        {
            "index1": "/some/path"
        },
        {
            "index2": "/some/path"
        }
    ],
    "heights": [
        {
            "index1": "67"
        },
        {
            "index2": "103"
        }
    ]
}

I'll have more arrays with same indexes so it'll be great if it'll be possible to parse it in one for. Thanks for any help

EDIT: Names of attributes are dynamic not static. It can be whatever, not just index1, index2, ...

1 Answer 1

1

You are using .index look up the data in the object from the heights array when you should use [index]. So this line:

console.log(data.heights[i].index);

Becomes:

console.log(data.heights[i][index]);
Sign up to request clarification or add additional context in comments.

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.