1

Schema (MongoDB):

key0: [
  {
     skey1: "sval1",
     skey2: "sval2"
  },
  {
     skey1: "sval3",
     skey2: "sval4"
  },
]

getJSON() Response:

[Object { skey1="val1", skey2="val2"}, Object { skey1="val3", skey2="val4"}]

Question:

How to access value of skey2 for each object?

jQuery (attempt):

my_var = results.key0

$.each(my_var, function(k,v) {
alert(v);
});

I think I need to do some sort of 'nested' $.each but not sure of the syntax.

2 Answers 2

1

This seems to work:

jsFiddle:

http://jsfiddle.net/rwone/Yg5cg/4/

// aim:  to alert value of 'skey2' for each object

myArray = [{ skey1:"val1", skey2:"val2"}, { skey1:"val3", skey2:"val4"}]

$.each(myArray, function(k,v) {
  alert(v.skey2);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

var my_var = [{
    skey1: "sval1",
    skey2: "sval2"
}, {
    skey1: "sval3",
    skey2: "sval4"
}, ];
var len = my_var.length;
for (var i = 0; i < len; i++) {
    $.each(my_var[i], function (k, v) {
        alert("key"+k+"value"+v);
    });
}

DEMO here.

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.