1

I have array1 that contains 1 char in each element.

What I need, is to get the value of array1[i].charCodeAt(); and put it in array2. Easy to do it, with a for statement.

    for(i=0;i<10;i++){
        y[i]= x[i].charCodeAt();
         }

I did some research but nothing that explain this case: Is it possible to populate array2 by some sort of destructuring, or what I am asking is not supported in js? For example:

    array1 =['a','b','c'];
    array2 = [];
    array2 = array1[].charCodeAt. 
conole.log('The first char has code ' + array2[0]); // The first letter has code 97.
tevemadar
  • 11,284
  • 3
  • 17
  • 44

2 Answers2

1

You aren't creating separate standalone variables, so destructuring isn't what you're looking for - but, you can use .map to transform the first array into the second:

const array1 =['a','b','c'];
const array2 = array1.map(char => char.charCodeAt(0));
console.log(array2);
CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
1

You can use Array.prototype.map():

const array1 = ['a','b','c'];
const array2 = array1.map(c => c.charCodeAt());

console.log(array2);
Yosvel Quintero Arguelles
  • 17,270
  • 4
  • 37
  • 42