I am looking to extract strings from an array and set them all to zero.
The original array consists of:
var myArray = ["apple", "banana", "cherry", "date"];
The expected outcome should be:
var apple = 0;
var banana = 0;
var cherry = 0;
var date = 0;
I was thinking of using a loop to go through the array, create new variables, and assign zero to them, but I'm unsure about the syntax.
What would be the best way to achieve this?
Just for your information, I plan to utilize these zeros as counters later on.
var myArray = ["apple", "banana", "cherry", "date"];
var counterArray = [];
$(document).ready(function() {
for (var i = 0; i < myArray.length; i++) {
var value = 0;
counterArray.push(value);
};
console.log(counterArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>