Javascript map method on array of string elements

后端 未结 8 456
悲哀的现实
悲哀的现实 2021-01-11 10:09

I am trying to understand how to implement the map method (rather than using a for loop) to check a string for palindromes and return boolean values for whether the mapped a

相关标签:
8条回答
  • 2021-01-11 10:56

    Apart from a few minor mistakes in your code, such as scope issues (you're referencing the "newArray" and "myArray" outside of the function in which they where defined, and therefore, getting "undefined")..

    The main issue you had is that you addressed the ENTIRE array inside the map function, while the whole concept is breaking things down to single elements (and then the function collects everything back to an array for you).

    I've used the "filter" function in my example, because it works in a similar manner and I felt that it does what you wanted, but you can change the "filter" to a "map" and see what happends.

    Cheers :)

    HTML:

    <body>
    
    <p id="bla">
    BLA
    </p>
    
    <p id="bla2">
    BLA2
    </p>
    
    </body>
    

    Javascript:

    function palindromeChecker(string) {
      var myString = string.toLowerCase();
      var myArray = myString.split(" ");
      var newArray = myArray.filter(function (item) {
        var reversedItem = item.split('').reverse().join('');
        return  item == reversedItem;
    });
    
    document.getElementById("bla").innerHTML = myArray;
    document.getElementById("bla2").innerHTML = newArray;
    
    }
    
    palindromeChecker("What pop did dad Drink today");
    
    0 讨论(0)
  • 2021-01-11 10:58

    Javascript map method on array of string elements by using split() function.

    let str = 'hello';
          
        str.split('').map((x)=> {
            console.log("|"+x+"|");
            return x;
        })
    
    0 讨论(0)
  • 2021-01-11 11:01

    newArray should include reversed version of theall items in myArray. After that, newArray should be reversed and joined with space in order to get the reversed version of the input string.

    Here is the code:

    function palindromeChecker(string) {
      var myString = string.toLowerCase();
      var myArray = myString.split(" ");
      var newArray = myArray.map(function (item) {
            return item.split("").reverse().join("");
        });
      console.log(newArray);
      return newArray.reverse().join(" ") === string;
    }
    
    console.log(palindromeChecker("dad did what"));
    
    0 讨论(0)
  • 2021-01-11 11:05

    try something like this:

    let str = 'hello';
    let tab = [...str];
    
    tab.map((x)=> {
        console.log("|"+x+"|");
        return x;
    })
    
    0 讨论(0)
  • 2021-01-11 11:07

    Map is a higher-order function available in ES5. I think your newArraywill contain an array of boolean values.

    In essence, map will iterate over every value in your array and apply the function. The return value will be the new value in the array. You can also use map and save the information you need somewhere else, and ignore the result of course.

    var arr = [1,2,3,4];
    var newArray = arr.map(function(i) {
      return i * 2;
    });
    //newArray = [2,4,6,8]
    
    0 讨论(0)
  • 2021-01-11 11:08

    Thanks for your input, all. This is the code I ended up with. I fixed the scope issues in the original post. My main problem was understanding the syntax of the map method. In particular, I could not understand from other online resources how to determine the value in the callback function. So, with much help from above I have placed the map method inside the palindromeChecker, and done all of the work on the array inside the map function.

    var palindromeChecker = function(string) {
    var newString = string.toLowerCase().split(' ');
    newString.map(function(item) {
      console.log(item.split('').reverse().join('') === item);
      });
    }; 
    
    palindromeChecker("What pop did dad drink today");
    
    //Returns false, true, true, true, false, false
    
    0 讨论(0)
提交回复
热议问题