-1

I would like to take an array of strings, get them out of an array and set them all to zero.

The initial array is:

var anArray = ["hello", "can", "you", "hear", "me"];

and the desired output would be:

var hello = 0;
var can = 0;
var you = 0;
var hear = 0;
var me = 0;

I thought by looping through the array, I could call a new variable and set equal to zero but I'm not sure how that is done.

How can this be done?

FWIW, the purpose of this is to use all of these zeros later as counters.

var anArray = ["hello", "can", "you", "hear", "me"];
var secondArray = 0;

$(document).ready(function() {
  for (var i = 0; i < anArray.length; i++) {
    var array[i] = 0;
    secondArray.push(array[i]);
  };
  console.log(secondArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
adin
  • 783
  • 3
  • 13
  • 27
  • 2
    If it's running in a browser, you can simply use `window[varname] = value`. However, I think that this transformation shouldn't be done at run-time. – Zeta Dec 29 '15 at 11:28
  • or you could make an array of objects? `obj word_count = {word: '*insert word here*', count: 0}` and make an array of those? – Irtza.QC Dec 29 '15 at 11:30
  • 1
    Making the counters this way is a really bad idea. Use an object like Irtza suggested (although not that complex: just do `var counters = { hello: 0, can: 0, ... }`) – JJJ Dec 29 '15 at 11:31
  • @Juhana The array is coming from data that's being loaded an API and then "cleaned" to search for unique names. Then the unique names are being used to count all the values associated. Should that still be an object? – adin Dec 29 '15 at 11:36
  • Yes, it definitely should. – JJJ Dec 29 '15 at 11:36
  • 1
    Why is this down voted? I did not know variables are members of the window object and have never used an object as counters. – adin Dec 29 '15 at 11:38

2 Answers2

2

I thought by looping through the array, I could call a new variable and set equal to zero but I'm not sure how that is done.

All the variables are members of window object. So, you can do this way:

var anArray = ["hello", "can", "you", "hear", "me"];
for (i = 0; i < anArray.length; i++)
  window[anArray[i]] = 0;

// Console.log
console.log(hello);
console.log(can);
console.log(you);
console.log(hear);
console.log(me);

And it works!!! :) Check your console while running the script.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    Remember to declare with var the for variables "for(`var i`.." to avoid unwanted code errors and performance issues, also cache the length of the array. – Sabaz Dec 29 '15 at 11:48
  • `.. , also cache the ..` no it's not an issue but a good practice in my opinion to store the length or other references. here a fast example of both (read the description) http://jsperf.com/test4452121963 . sorry if I was rude, I did not mean to – Sabaz Dec 29 '15 at 15:20
  • @Saba You were **never rude** and I learnt something new. `:)` Thank you so much. – Praveen Kumar Purushothaman Dec 29 '15 at 15:20
0

You can do in this way also. Fiddle here

Reference

function testData() {
  var anArray = ["hello", "can", "you", "hear", "me"];
  var firstObj = {};
  for (var i = 0; i < anArray.length; i++) {
    firstObj[anArray[i]] = 0;
  }
  return firstObj;
}

alert(JSON.stringify(testData()));
Community
  • 1
  • 1
Kaushik
  • 2,072
  • 1
  • 23
  • 31