Home>
I want to delete the first character with

.map ().

Error message

The result is as follows on the console.

[undefined, undefined]
Applicable source code
var arr = ['0a', '0b']
var result = arr.map (function (e) {e.slice (1)});
console.log ('result', result);

The following were confirmed.

// Check that the first character has been deleted
str = '0a';
var a = str.slice (1);
console.log ('a', a);
// Confirm that "0a" and "0b" are passed to "e"
var arr = ['0a', '0b']
var test = arr.map (function (e) {console.log ('e', e);});


The usage of slice is correct, and the character string to be deleted can be passed as "e". Why does the above problem occur? Can someone tell me?

  • Answer # 1

    Did not return. It was confused with the arrow function.

  • Answer # 2

    Return or use arrows

    var result = arr.map (function (e) {return e.slice (1);});
    Or
    var result = arr.map (e =>e.slice (1));

  • Answer # 3

    return.

    var result = arr.map (function (e) {e.slice (1)});

Related articles