0

I have an array of objects like this -

    [{"name":"admission[year]","value":"2011-12"},{"name":"admission[class]","value":"Nursery"}]

How can i access the name value pairs like admission[year] - 2011-12, and admission[class] - nursery in javascript.

3 Answers 3

2
var x = [{"name":"admission[year]","value":"2011-12"},{"name":"admission[class]","value":"Nursery"}]
var i, len = x.length;
for(i = 0; i < len; i++)
    console.log(x[i].name + ': ' + x[i].value);

Outputs:

admission[year]: 2011-12
admission[class]: Nursery

IE. x[0].name === "admission[year]" and x[1].value === "Nursery"

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

1 Comment

Instead of i and len you can use "for (i in x)" - no boundary condition exception.
0
/*

using jquery you can do something like this 

*/

$.each( ['a','b','c'], function(key, value){
   alert( "Index #" + key + ": " + value );
 });

1 Comment

The question doesn't mention jQuery.
0
var arr = [{ "name": "admission[year]", "value": "2011-12" }, { "name": "admission[class]", "value": "Nursery"}];
for (element in arr) {
    var combinedValue = arr[element].name + ' ' + arr[element].value;
    alert(combinedValue);
}

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.