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.

Box Box Box Box
5,39110 gold badges52 silver badges71 bronze badges
asked Nov 21, 2011 at 5:38

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"

pradeek
22.2k2 gold badges33 silver badges32 bronze badges
answered Nov 21, 2011 at 5:43

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 );
 });
answered Nov 21, 2011 at 5:44

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);
}
bluish
27.5k28 gold badges125 silver badges185 bronze badges
answered Nov 21, 2011 at 5:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.