Numeric values with a for loop

Hello Campers,

I have created an array with some data on it. Also i created a Method called Object.keys that will returns an array of a given object’s own enumerable property. Its there is a way i can do it with regular for loop, I know it might sounds a basic question but i’m trying hard to get the same result with a regular foor loop.

Code:

var uncomArray = [
   "Learn PHP",
   "Learn ReactJs",
   "Learn MySQL"
];
var numericValue = Object.keys(uncomArray);
console.log(numericValue); // output: ["0", "1", "2"]
// with regular for loop
for( var i = 0; i < uncomArray.length; i++ ){
    console.log(i, uncomArray[i],uncomArray); 
}
//output: 
0 "Learn PHP" ["Learn PHP", "Learn ReactJs", "Learn MySQL"]
1 "Learn ReactJs" ["Learn PHP", "Learn ReactJs", "Learn MySQL"]
2 "Learn MySQL" ["Learn PHP", "Learn ReactJs", "Learn MySQL"]

codepen:

@imendieta In the code you posted above, you the i variable is the key, so you can just create an array empty array before the for loop and add each value of i to the array. The only difference would be that i would be a number instead of a string, so if you really want the string, you would need to convert it to a string before adding it to the array.

@RandellDawson,

Here is what i got but it seems i still got something wrong im working on it.
codepen

The array you need to add the value is newArr. Also, the toString method returns a new value and does not change the value of the variable upon which it is called.

Hi @RandellDawson,

So i got this. Yes at one point i thought about i be the key because its the index. so i at first i had the idea you mention, but i wasent sure, plus i was having trouble and i did not know we had to convert the array numbers index into strings. because Object.keys do have it build in already, but i notice that the ouput of it was strings i just did not know we supposed to change it. i have updated the content. Can you please validate if i did ok. thanks :slight_smile:

I think you can combine a couple of lines as long as you make the variable name meaningful.

var uncomArray = ["Learn PHP", "Learn ReactJs", "Learn MySQL"];

var keys = [];

for (var i = 0; i < uncomArray.length; i++) {
  var stringIndex = i.toString();
  keys.push(stringIndex);
}
console.log(keys);  // ["0", "1", "2"]

Notice the variable is named stringIndex because it contains the index converted to a string.

1 Like

Similar to using a for loop, you could use the map method to accomplish this same task.

var uncomArray = ["Learn PHP", "Learn ReactJs", "Learn MySQL"];

var keys = uncomArray.map(function(elem, index){
  return index.toString();  
});

console.log(keys); // ["0", "1", "2"]
1 Like

Yes the map method its really good to i can see your using 2 because one its the element and you need a second one witch is the index thats the one we need for having the output. yes that one also its a good one as well, and works the same way. wow very interesting to know. i will give some more practice one thos 2 new methods.
thank you Randell your always there for us to learn and put it to practice.