0

I have one array as below in JavaScript :

Actual array in JavaScript: array('123456' => '1', '456789' => '0', '789123' =>'1', '4456784' => '0', 'so on...' => '0');

Wanted result array in JavaScript: array('123456' => '1', '456789' => '1', '789123' =>'1', '4456784' => '1', 'so on...' => '1');

Is there any way to change JavaScript array all values as fix like 1 or 0(In wanted result array) without using any loops like(for, .each, etc...).

Please suggest any idea if there is any possibilities in this. Thank you in advance!

5
  • You're showing a PHP array in a Dictionary form, Javascript doesn't support Dictionary (yet). Commented Nov 18, 2014 at 7:30
  • 1
    JS supports dictionary as objects... The solution is a loop... Why don't you want to use a loop to do the loop job ? You could use a function that do a loop :D Commented Nov 18, 2014 at 7:32
  • Use objects as dictionaries. Creating this object with Object.create(null) helps. And you need to use a for in loop to process it. Commented Nov 18, 2014 at 7:34
  • you can stringify/replace/parse, but a loop is faster Commented Nov 18, 2014 at 7:34
  • There are 1092 elements in array, and maybe it can increase more then this, so is not loop takes more time? So i need shorter way then loop to assign same value in all elements of array. Commented Nov 18, 2014 at 7:40

1 Answer 1

1

you can define your array in javascript:

var arrayData = new Array({'key':'123456','value':'1'}, {'key':'456789','value':'0'},{'key':'789123','value':'1'}, {'key':'4456784','value':'0'});

and use array like that :

 $.each(arrayData, function(index,data){
     alert(arrayData[index].key);
     alert(arrayData[index].value);
 })

and set value in array according to your condition.

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

2 Comments

Suppose I have javascript array like you gave, var arrayData = new Array({'key':'123456','value':'1'}, {'key':'456789','value':'0'},{'key':'789123','value':'1'}, {'key':'4456784','value':'0'}); And now if i wanted replace its value without using $.each, then how can it be?
apply check on key and change its value accordingly. like : arrayData[index].value = 10

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.