1

Im still needing help with this, and have edited the jsfiddle post to show my problem. http://jsfiddle.net/7ztEf/6/

I want to return number to associated index value [0] =0 [1]=1 as you can see the index string returns all numbers. Thanks again Paul

I have a number generator script that returns values to DIV ID's. I need to hook into this somehow, to enable replacing color based upon the number value i.e. > 1 && <= 20 = red etc.

function myNumbers(numbers, type) {
    for (var x in numbers) {
       document.getElementById(type + x).innerHTML = numbers[x];
    } 
}

This script fills each of the DIVs named num0 ... num3 with a random number.

I have managed to query the first value of numbers[x] but need to set an index order to loop through the rest, or something.

4
  • what is numbers? is it an array or object Commented Mar 4, 2013 at 3:40
  • Your code seems fine, Can you check this demo and tell what is the desired o/p jsfiddle.net/arunpjohny/7ztEf Commented Mar 4, 2013 at 3:42
  • You should not use for..in with arrays, it will iterate over all enumerable properties (including inherited properties), not just numeric indices. Also, order isn't guaranteed (but that probably isn't an issue here). Use a normal for loop with incrementing index, or forEach with a shim for older browsers. Commented Mar 4, 2013 at 4:02
  • Hi thanks Johny, using the jsfiddle you posted I am wanting to query the value of myNumbers, if value is 1 then div bg =blue, if value is two bg =red etc Commented Mar 4, 2013 at 10:28

2 Answers 2

0

Use Array.forEach.

numbers.forEach(function (number, index) {...})
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use for..in for arrays. They're meant to be used on objects so using for..in on arrays will return such things as the length element.

Either use forEach as ethagnawl mentioned or use the traditional for loop:

for (var x=0; x < numbers.length; x++) {
   document.getElementById(type + x).innerHTML = numbers[x];
}

6 Comments

Thanks @slebetman! This still returns [0] = 0,1,2,3. I need to list index [0][1][2] and the numbers value. Cheers P
Very close. There are still things you're misunderstanding. I just changed a few characters in you code to fix it: jsfiddle.net/h9AS6/1
Read the differences between my jsfiddle and yours carefully.
Thanks again @slebetman that has helped me, now I can access the array index. Could you recommend how I should compare the returned index values? ie for each index[0][1][2][3] if myNumbers == 1 than do something else if myNumbers ==2 do something. JS is not my thing so appreciate any help!!! cheeers P
numbers[x] is the value x is the key and numbers is the array. So just do if(numbers[x] == 1) ... etc
|

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.