Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a JavaScript object like the following:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

Now I want to loop through all p elements (p1,p2,p3...) and get their keys and values. How can I do that?

I can modify the JavaScript object if necessary. My ultimate goal is to loop through some key value pairs and if possible I want to avoid using eval.

share|improve this question
5  
I changed JSON to JavaScript (object) to avoid confusing of object literals and JSON. – Felix Kling Mar 29 '12 at 16:48
    

23 Answers 23

up vote 2440 down vote accepted

You can use the for-in loop as shown by others. However, you also want to make sure that the key you get is an actual property of an object, and doesn't come from the prototype:

for (var key in p) {
  if (p.hasOwnProperty(key)) {
    console.log(key + " -> " + p[key]);
  }
}
share|improve this answer
18  
Would propose that you change the alert line just for clarity to alert(key + " -> " + JSON.stringify(p[key])); – Steve Midgley Aug 18 '11 at 22:03
29  
Can you explain the need for hasOwnProperty? What you mean by prototype? – kamaci Aug 22 '11 at 12:46
175  
In javascript, every object has a bunch of built-in key-value pairs that have meta-information. When you loop through all the key-value pairs for an object you're looping through them too. hasOwnPropery() filters these out. – danieltalsky Jan 27 '12 at 15:56
26  
Actually, For...in is not deprecated. For each...in is. But I really like the term archaeologists...I'm going to have to start using that. – Ben Y Feb 27 '14 at 16:08
5  
Don't use obj.hasOwnProperty(prop) for membership checks. Use Object.prototype.hasOwnProperty.call(obj, prop) instead, which works even if the "set" contains the value "hasOwnProperty". Source: stackoverflow.com/questions/7958292/… – Michael Mar 4 '15 at 21:13

You have to use the for-in loop

But be very careful when using this kind of loop, because this will loop all the properties along the prototype chain.

Therefore, when using for-in loops, always make use of the hasOwnProperty method to determine if the current property in iteration is really a property of the object you're checking on:

for (var prop in p) {
    if (!p.hasOwnProperty(prop)) {
        //The current property is not a direct property of p
        continue;
    }
    //Do your logic with the property here
}
share|improve this answer
25  
This is better than levik's solution because it allows the main logic to be only one nested loop in rather than two; making for easier to read code. Although I'd loose the the brackets around the continue; they are superfluous. – SystemicPlural Apr 6 '11 at 9:55
38  
I would not remove the { } personally because an if without them makes it a little unclear what is part of the if and what is not. But I guess that's just a matter of opinion :) – pimvdb Aug 5 '11 at 12:01
23  
Yes, I prefer keeping the { } mainly to avoid confusion if one later on needs to add something to the if scope. – Andreas Grech Aug 5 '11 at 12:21
7  
Reading my previous comment, I realized that I didn't use the correct terms, because I said "if scope"; but keep in mind that JavaScript only has function scope. So what I actually meant was "if block". – Andreas Grech Nov 11 '11 at 11:08
1  
eomeroff, if you're really concerned about that, you could always do something like: Object.prototype.hasOwnProperty.call(p, prop) However, this too can't protect against manipulations to Object.prototype... – jordancpaul Oct 15 '13 at 7:37

The question won't be complete if we don't mention about alternative methods for looping through objects.

Nowadays many well known JavaScript libraries provide their own methods for iterating over collections, i.e. over arrays, objects, and array-like objects. These methods are convenient to use and are entirely compatible with any browser.

  1. If you work with jQuery, you may use jQuery.each() method. It can be used to seamlessly iterate over both objects and arrays:

    $.each(obj, function(key, value) {
        console.log(key, value);
    });
    
  2. In Underscore.js you can find method _.each(), which iterates over a list of elements, yielding each in turn to a supplied function (pay attention to the order of arguments in iteratee function!):

    _.each(obj, function(value, key) {
        console.log(key, value);
    });
    
  3. Lo-Dash provides several methods for iterating over object properties. Basic _.forEach() (or it's alias _.each()) is useful for looping through both objects and arrays, however (!) objects with length property are treated like arrays, and to avoid this behavior it is suggested to use _.forIn() and _.forOwn() methods (these also have value argument coming first):

    _.forIn(obj, function(value, key) {
        console.log(key, value);
    });
    

    _.forIn() iterates over own and inherited enumerable properties of an object, while _.forOwn() iterates only over own properties of an object (basically checking against hasOwnProperty function). For simple objects and object literals any of these methods will work fine.

Generally all described methods have the same behaviour with any supplied objects. Besides using native for..in loop will usually be faster than any abstraction, such as jQuery.each(), these methods are considerably easier to use, require less coding and provide better error handling.

share|improve this answer
4  
To get to the value: $.each(obj, function (key, value) { console.log(value.title); }); – Ravi Ram Jun 8 '13 at 14:41

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

ES2016 adds for...of:

for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

ES2017 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

share|improve this answer
8  
Why didn't the standard provide Object.forEach(obj, function (value, key) {...})? :( Certainly obj.forEach(function...) would be shorter and complement Array.prototype.forEach, but that would risk having objects define their own forEach property. I suppose Object.keys guards against the callback modifying the object's keys. – David Harkness Jun 23 '14 at 20:36
3  
Object.forEach = function (obj, callback) { Object.keys(obj).forEach(function (key) { callback(obj[key], key); }); } – David Harkness Jun 23 '14 at 20:41
2  
@DavidHarkness There is Object.entries in ES2017. There you can do the following: Object.entries(obj).map/forEach(([key, value]) => console.log(key, value)) ([key, value] is array destructuring, to access both items directly. And you have to wrap the parameters in additional parens.) – Andreas Linnert Jul 14 '16 at 12:50

In ECMAScript 5 you have new approach in iteration fields of literal - Object.keys

More information you can see on MDN

My choice is below as a faster solution in current versions of browsers (Chrome30, IE10, FF25)

var keys = Object.keys(p),
    len = keys.length,
    i = 0,
    prop,
    value;
while (i < len) {
    prop = keys[i];
    value = p[prop];
    i += 1;
}

You can compare performance of this approach with different implementations on jsperf.com:

Browser support you can see on Kangax's compat table

For old browser you have simple and full polyfill

UPD:

performance comparison for all most popular cases in this question on perfjs.info:

object literal iteration

share|improve this answer
    
Indeed, I just wanted to post this method. But you beat me to it :( – Jamie Hutber Mar 20 '14 at 23:05
    

You can just iterate over it like:

for (var key in p) {
  alert(p[key]);
}

Note that key will not take on the value of the property, it's just an index value.

share|improve this answer
4  
This is repeated and not even entirely correct. You need to have a check of hasOwnProperty to make this work properly – Vatsal Jun 2 '16 at 20:18

via prototype with forEach() which should skip the prototype chain properties:

Object.prototype.each = function(f) {
    var obj = this
    Object.keys(obj).forEach( function(key) { 
        f( key , obj[key] ) 
    });
}


//print all keys and values
var obj = {a:1,b:2,c:3}
obj.each(function(key,value) { console.log(key + " " + value) });
// a 1
// b 2
// c 3
share|improve this answer
2  
Be careful with the prototype: obj = { print: 1, each: 2, word: 3 } produces TypeError: number is not a function. Using forEach to match the similar Array function may reduce the risk somewhat. – David Harkness Jun 23 '14 at 21:40

After looking through all the answers in here, hasOwnProperty isn't required for my own usage because my json object is clean; there's really no sense in adding any additional javascript processing. This is all I'm using:

for (var key in p) {
    console.log(key + ' => ' + p[key]);
    // key is key
    // value is p[key]
}
share|improve this answer
16  
Whether the JSON object is clean or not is irrelevant. If at any other time some code sets a property on Object.prototype, then it will be enumerated by for..in. If you are sure you are not using any libraries that do that, then you don't need to call hasOwnProperty. – G-Wiz Jan 13 '12 at 20:15
1  
@G-Wiz If it's your own code you're quite able to certify it's clean. – Michiel van der Blonk May 14 '15 at 22:16
    
Assumptions make asses of us all. – A.B. Carroll Jun 1 '15 at 20:24
3  
It can be completely clean if created with Object.create(null) – Juan Mendes Apr 14 '16 at 11:37

Object.keys(obj) : Array

retrieves all string-valued keys of all enumerable own (non-inherited) properties.

So it gives the same list of keys as you intend by testing each object key with hasOwnProperty. You don't need that extra test operation than and Object.keys( obj ).forEach(function( key ){}) is supposed to be faster. Let's prove it:

var uniqid = function(){
			var text = "",
					i = 0,
					possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			for( ; i < 32; i++ ) {
					text += possible.charAt( Math.floor( Math.random() * possible.length ) );
			}
			return text;
		}, 
		CYCLES = 100000,
		obj = {}, 
		p1,
		p2,
		p3,
		key;

// Populate object with random properties
Array.apply( null, Array( CYCLES ) ).forEach(function(){
	obj[ uniqid() ] = new Date()
});

// Approach #1
p1 = performance.now();
Object.keys( obj ).forEach(function( key ){
	var waste = obj[ key ];
});

p2 = performance.now();
console.log( "Object.keys approach took " + (p2 - p1) + " milliseconds.");

// Approach #2
for( key in obj ) {
	if ( obj.hasOwnProperty( key ) ) {
		var waste = obj[ key ];
	}
}

p3 = performance.now();
console.log( "for...in/hasOwnProperty approach took " + (p3 - p2) + " milliseconds.");

In my Firefox I have following results

  • Object.keys approach took 40.21101451665163 milliseconds.
  • for...in/hasOwnProperty approach took 98.26163508463651 milliseconds.

PS. on Chrome the difference even bigger http://codepen.io/dsheiko/pen/JdrqXa

PS2: In ES6 (EcmaScript 2015) you can iterate iterable object nicer:

let map = new Map().set('a', 1).set('b', 2);
for (let pair of map) {
    console.log(pair);
}

// OR 
let map = new Map([
    [false, 'no'],
    [true,  'yes'],
]);
map.forEach((value, key) => {
    console.log(key, value);
});

share|improve this answer
    
if you don't feel like letting go of the {} notation, you can still use of without creating Maps – Hashbrown Jun 28 '16 at 9:47
var p =
    {
        "p1": "value1",
        "p2": "value2",
        "p3": "value3"
    };

for (var key in p) 
{
    if (p.hasOwnProperty(key))
    {
    alert(key + " = " + p[key]);
    }
}
---------------------------
---------------------------
Output:
p1 = values1
p2 = values2
p3 = values3
share|improve this answer

It's interesting people in these answers have touched on both Object.keys() and for...of but never combined them:

var map = {well:'hello', there:'!'};
for (let key of Object.keys(map))
    console.log(key + ':' + map[key]);

You can't just for...of an Object because it's not an iterator, and for...index or .forEach()ing the Object.keys() is ugly/inefficient.
I'm glad most people are refraining from for...in (with or without checking .hasOwnProperty()) as that's also a bit messy, so other than my answer above, I'm here to say...


You can make ordinary object associations iterate! Behaving just like Maps with direct use of the fancy for...of
DEMO working in Chrome and FF (I assume ES6 only)

var ordinaryObject = {well:'hello', there:'!'};
for (let pair of ordinaryObject)
    //key:value
    console.log(pair[0] + ':' + pair[1]);

//or
for (let [key, value] of ordinaryObject)
    console.log(key + ':' + value);

So long as you include my shim below:

//makes all objects iterable just like Maps!!! YAY
//iterates over Object.keys() (which already ignores prototype chain for us)
Object.prototype[Symbol.iterator] = function() {
    var keys = Object.keys(this)[Symbol.iterator]();
    var obj = this;
    var output;
    return {next:function() {
        if (!(output = keys.next()).done)
            output.value = [output.value, obj[output.value]];
        return output;
    }};
};

Without having to create a real Map object that doesn't have the nice syntactic sugar.

var trueMap = new Map([['well', 'hello'], ['there', '!']]);
for (let pair of trueMap)
    console.log(pair[0] + ':' + pair[1]);

In fact, with this shim, if you still wanted to take advantage of Map's other functionality (without shimming them all in) but still wanted to use the neat object notation, since objects are now iterable you can now just make a Map from it!

//shown in demo
var realMap = new Map({well:'hello', there:'!'});

For those who don't like to shim, or mess with prototype in general, feel free to make the function on window instead, calling it something like getObjIterator() then;

var realMap = new Map(getObjIterator({well:'hello', there:'!'}))

or

for (let pair of getObjIterator(ordinaryObject))

There's no reason why that wouldn't work.

Welcome to the future.

share|improve this answer
    
Why do you have no upvotes? Is there something you are missing? – HelpMeStackOverflowMyOnlyHope Jul 22 '16 at 6:11
    
I only put the answer in a short while ago. A bunch of my answers are like that (new tech on old answers), they'll roll in eventually – Hashbrown Jul 22 '16 at 6:47
1  
Case in point. So long as people scroll down and find it helpful, that's all that matters. Usually it's me trying to do something, not liking the stuff I see online, end up figuring it out, then I come back to share. It's good doco, I've actually come across my own answers before googling things I completely forgot about! – Hashbrown Jul 22 '16 at 6:57
    
@HelpMeStackOverflowMyOnlyHope Personally I do not like modifying the prototypes of objects I did not define myself. – Janus Troelsen Sep 30 '16 at 10:19
    
@JanusTroelsen did you even read the whole answer? For those who don't like to shim, or mess with prototype in general, feel free to make the function on window instead, calling it something like getObjIterator() then; – Hashbrown Sep 30 '16 at 12:58

You can add a simple forEach function to all objects, so you can automatically loop through any object:

Object.defineProperty(Object.prototype, 'forEach', {
    value: function (func) {
        for (var key in this) {
            if (!this.hasOwnProperty(key)) {
                // skip loop if the property is from prototype
                continue;
            }
            var value = this[key];
            func(key, value);
        }
    },
    enumerable: false
});

For those people who don't like the "for ... in"-method:

Object.defineProperty(Object.prototype, 'forEach', {
    value: function (func) {
        var arr = Object.keys(this);
        for (var i = 0; i < arr.length; i++) {
            var key = arr[i];
            func(key, this[key]);
        }
    },
    enumerable: false
});

Now, you can simple call:

p.forEach (function(key, value){
    console.log ("Key: " + key);
    console.log ("Value: " + value);
});

If you don't want to get conflicts with other forEach-Methods you can name it with your unique name.

share|improve this answer
    
Modifying the prototypes of built in objects (like Object) is generally considered an anti pattern because it can easily cause conflicts with other code. So wound not recommend doing it this way. – Moritz Jan 6 at 13:06
for(key in p) {
  alert( p[key] );
}

Note: you can do this over arrays, but you'll iterate over the length and other properties, too.

share|improve this answer
3  
When using a for loop like that, key will just take on an index value, so that will just alert 0, 1, 2, etc... You need to access p[key]. – Bryan Mar 26 '09 at 6:07
    
It is the slowest method of array iteration in JavaScript. You can check this on your computer - Best way to iterate over Arrays in JavaScript – Pencroff Dec 5 '13 at 12:15
5  
@Pencroff: the problem is that the question is not about looping through arrays... ;) – Sk8erPeter Jan 1 '14 at 0:55
    
This is something I don't understand on stackoverflow. Richard gave the correct answer, and he was the first one giving that answer, but he did not get any +1? @Bryan var p = {"p1":"q","p2":"w"}; for(key in p) { alert( key ); } is popping "p1" and "p2" in alerts, so whats wrong about that??? – Sebastian Aug 5 '14 at 6:43
3  
I think the main difference is the quality: the other answers not only tell how, but also tell the caveats (e.g., the prototype) and how to deal with those caveats. IMHO, those other answers are better than mine :). – Richard Levasseur Aug 6 '14 at 16:41

If you want to iterate over non-enumerable properties as well, you can use Object.getOwnPropertyNames(obj) to return an array of all properties (enumerable or not) found directly upon a given object.

var obj = Object.create({}, {
  // non-enumerable property
  getFoo: {
    value: function() { return this.foo; },
    enumerable: false
  }
});

obj.foo = 1; // enumerable property

Object.getOwnPropertyNames(obj).forEach(function (name) {
  document.write(name + ': ' + obj[name] + '<br/>');
});

share|improve this answer
2  
This is fantastic, thank you for posting this answer. I needed to introspect an Error object and couldn't get at the properties in a loop or a _.forIn(err) call. Using Object.getOwnPropertyNames(err) allowed me to access all the parts of the Error that I couldn't get at before. Thanks! – Pierce Mar 11 '16 at 19:49

Only JavaScript code without dependencies:

var p = {"p1": "value1", "p2": "value2", "p3": "value3"};
keys = Object.keys(p);   // ["p1", "p2", "p3"]

for(i = 0; i < keys.length; i++){
  console.log(keys[i] + "=" + p[keys[i]]);   // p1=value1, p2=value2, p3=value3
}
share|improve this answer

Loops can be pretty interesting when using pure JavaScript. It seems that only ECMA6 (New 2015 JavaScript specification) got the loops under control. Unfortunately as I'm writing this, both Browsers and popular Integrated development environment (IDE) are still struggling to support completely the new bells and whistles.

At a glance here is what a JavaScript object loop look like before ECMA6:

for (var key in object) {
  if (p.hasOwnProperty(key)) {
    var value = object[key];
    console.log(key); // This is the key;
    console.log(value); // This is the value;
  }
}

Also, I know this is out of scope with this question but in 2011, ECMAScript 5.1 added the forEach method for Arrays only which basically created a new improved way to loop through arrays while still leaving non iterable objects with the old verbose and confusing for loop. But the odd part is that this new forEach method does not support break which led to all sorts of other problems.

Basically in 2011, there is not a real solid way to loop in JavaScript other than what many popular libraries (jQuery, Underscore, etc.) decided to re-implement.

As of 2015, we now have a better out of the box way to loop (and break) any object type (including Arrays and Strings). Here is what a loop in JavaScript will eventually look like when the recommendation becomes mainstream:

for (let [key, value] of Object.entries(object)) {
    console.log(key); // This is the key;
    console.log(value); // This is the value;
}

Note that most browsers won't support the code above as of June 18th 2016. Even in Chrome you need to enable this special flag for it to work: chrome://flags/#enable-javascript-harmony

Until this becomes the new standard, the old method can still be used but there are also alternatives in popular libraries or even lightweight alternatives for those who aren't using any of these libraries.

share|improve this answer
    
Could you provide a fiddle of this working? Here is my attempt. jsfiddle.net/abalter/sceeb211 – abalter Jun 18 '16 at 6:24
    
@abalter Sorry I realised I had a typo in my code. I fixed it and updated your JsFiddle here: jsfiddle.net/sceeb211/2 – Nicolas Bouvrette Jun 18 '16 at 12:56
    
I'm in chrome and getting Uncaught TypeError: Object.entries is not a function. Is it not implemented in chrome yet? – abalter Jun 18 '16 at 16:07
    
@abalter It is. Make sure you have Chrome version 51 and that you have enabled the flag as explained in my edit and Jsfiddle comments. You can check the details here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Nicolas Bouvrette Jun 18 '16 at 16:39
    
Sorry I missed that about the flag. I see it's not a fully implemented feature yet. – abalter Jun 19 '16 at 5:39

Considering ES6 I'd like to add my own spoon of sugar and provide one more approach to iterate over object's properties.

Since plain JS object isn't iterable just out of box, we aren't able to use for..of loop for iterating over its content. But no one can stop us to make it iterable.

Let's we have book object.

let book = {
  title: "Amazing book",
  author: "Me",
  pages: 3
}

book[Symbol.iterator] = function(){

  let properties = Object.keys(this); // returns an array with property names
  let counter = 0;
  let isDone = false;

  let next = () => {
    if(counter >= properties.length){
      isDone = true;
    }
    return { done: isDone, value: this[properties[counter++]] }
  }

  return { next };
}

Since we've made it we can use it this way:

for(let pValue of book){
  console.log(pValue);
}
------------------------
Amazing book
Me
3

Or if you know the power of ES6 generators, so you certainly can make the code above much shorter.

book[Symbol.iterator] = function *(){

  let properties = Object.keys(this);
  for (let p of properties){
    yield this[p];
  }

}

Sure, you can apply such behavior for all objects with making Object iterable on prototype level.

Object.prototype[Symbol.iterator] = function() {...}

Also, objects that comply with the iterable protocol can be used with the new ES2015 feature spread operator thus we can read object property values as an array.

let pValues = [...book];
console.log(pValues);
-------------------------
["Amazing book", "Me", 3]

Or you can use destructuring assignment:

let [title, , pages] = book; // notice that we can just skip unnecessary values
console.log(title);
console.log(pages);
------------------
Amazing book
3

You can check out JSFiddle with all code I've provided above.

share|improve this answer
    
I found the code will generate the values but without keys. Is it possible to iterate the values with keys? – Anson Wong Sep 8 '16 at 3:34
    
Yes, you can. Just return "yield [key, obj[key]];" from your generator function and then use it like the following "for(let [ key, value ] of {}) { }" – Artyom Pranovich Sep 8 '16 at 13:33

If anybody needs to loop through arrayObjects with condition:

var arrayObjects = [{"building":"A", "status":"good"},{"building":"B","status":"horrible"}];

for (var i=0; i< arrayObjects.length; i++) {
  console.log(arrayObjects[i]);
  
  for(key in arrayObjects[i]) {      
    
      if (key == "status" && arrayObjects[i][key] == "good") {
        
          console.log(key + "->" + arrayObjects[i][key]);
      }else{
          console.log("nothing found");
      }
   }
}

share|improve this answer

Since es2015 is getting more and more popular I am posting this answer which include usage of generator and iterator to smoothly iterate through keys,values pairs. As it is possible in other languages for instance Ruby.

Ok here is a code:

let MyObject = {
    'a': 'Hello',
    'b': 'it\'s',
    'c': 'me',
    'd': 'you',
    'e': 'looking',
    'f': 'for',
    [Symbol.iterator]: function* () {
        let properties = Object.keys(this);
        for (let i of properties) {
            yield [i, this[i]];
        }
    }
};

for (let [k, v] of MyObject) {
    console.log(`Here is key ${k} and here is value ${v}`);
}

All information about how can you do an iterator and generator you can find at developer mozilla page.

If you need further explanation please let me know in the comments rather than giving downvotes. Hope I helped someone :)

share|improve this answer

I would do this rather than checking obj.hasOwnerProperty within every for ... in loop.

var obj = {a : 1};
for(var key in obj){
    //obj.hasOwnProperty(key) is not needed.
    console.log(key);
}
//then check if anybody has messed the native object. Put this code at the end of the page.
for(var key in Object){
    throw new Error("Please don't extend the native object");
}
share|improve this answer

In ES6 we have well-known symbols to expose some previously internal methods, you can use it to define how iterators work for this object:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3",
    *[Symbol.iterator]() {
        yield *Object.keys(this);
    }
};

[...p] //["p1", "p2", "p3"]

this will give the same result as using for...in es6 loop.

for(var key in p) {
    console.log(key);
}

But its important to know the capabilities you now have using es6!

share|improve this answer

Since ES2015 you can use the for of loop, to access the element directly:

// before ES2015
for(var key of elements){
  console.log(elements[key]);
}


// ES2015
for(let element of elements){
  console.log(element);
}

Hope this helps someone.

share|improve this answer
2  
Not just does it not help, it's not accurate. For...of only works if elements defines an iterator. That's true of Arrays, Sets, and Maps, but not of Objects. – Evan Carroll Jun 8 '16 at 17:29
    
you can actually make this work, @EvanCarroll – Hashbrown Jun 28 '16 at 9:43

Besause the asker's ['ultimate goal is to loop through some key value pairs'] and finally don't looking for a loop.

var p ={"p1":"value1","p2":"value2","p3":"value3"};
if('p1' in p){
  var val=p['p1'];
  ...
}
share|improve this answer
2  
No! He wrote "Now I want to loop through all p elements" so he realy need a loop like in his accepted answer. – Sebastian Aug 5 '14 at 6:40
    
@Sebastian I thought he knows which values he like to ask for. – B.F. Aug 6 '14 at 6:43

protected by VisioN Feb 27 '13 at 8:52

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.