字符串元素数组上的Javascript映射方法 [英] Javascript map method on array of string elements

查看:84
本文介绍了字符串元素数组上的Javascript映射方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何实现map方法(而不是使用for循环)来检查字符串中的回文,并返回布尔值来确定反转的映射数组元素是否与原始数组元素相同.我似乎无法理解map方法的语法.如何使地图在原始数组中的每个元素上起作用?有什么价值?这是我的工作代码,仅记录未定义的值:

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 array elements reversed are the same as the original array elements. I cannot seem to understand the syntax of the map method. How do I get the map to function on each element in the original array? What is the value? Here is my working code, which is only logging a value of undefined:

function palindromeChecker(string) {
    var myString = string.toLowerCase();
    var myArray = myString.split(" ");
    var newArray = myArray.map(function (item) {
        item.split("").reverse().join("");
        return newArray === myArray;
    });
}

console.log(palindromeChecker("What pop did dad Drink today"));

以下是小提琴的链接: https://jsfiddle.net/minditorrey/3s6uqxrh/1/

Here is a link to the fiddle: https://jsfiddle.net/minditorrey/3s6uqxrh/1/

这里有一个相关的问题:

There is one related question here:

JavaScript数组映射方法回调参数

但是当使用map方法对字符串数组执行功能时,它并不能解决我对map方法语法的困惑.

but it doesn't answer my confusion about the syntax of the map method when using it to perform a function on an array of strings.

推荐答案

map方法将在字面上映射"函数调用到数组中的每个元素上,以此作为增加每个整数值的简单示例以1:

The map method will literally 'map' a function call onto each element in the array, take this as a simple example of increasing the value of each integer in an array by 1:

var items = [1,2,3];
items.map(function(item) { 
  return item + 1;
});

// returns [2,3,4]

在您的情况下,您尝试使用map接受或拒绝字符串(如果是回文),因此一个简单的实现可能是:

In your case, you are trying to use map to accept or reject a string if it's a palindrome, so a simple implementation might be:

var items = ['mum', 'dad', 'brother'];
items.map(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns [true, true, false]

我不确定100%使用map的原因,因为如果您只是尝试过滤数组并删除不是回文的字符串,则可能应该使用filter方法,的工作方式相同,但会删除所有返回false的内容:

I'm not 100% sure of your reasons for using map, because if you were trying to just filter the array and remove the strings that aren't palindromes, you should probably use the filter method instead, which works in the same way, but would remove any that return false:

var items = ['mum', 'dad', 'brother'];
items.filter(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns ['mum', dad']

在您的情况下,您首先要拆分字符串以获取字符数组;您可能还希望将该字符串设置为小写并删除标点符号,因此实现可能是:

In your case you are splitting a string first to get your array of characters; you may also want to make that string lower case and remove punctuation, so an implementation might be:

var string = 'I live at home with my Mum, my Dad and my Brother!';
var items = string.toLowerCase().replace(/[^a-z0-9-\s]+/, '').split(' ');
items.filter(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns ['i', 'mum', dad']

如对问题的评论之一所述,如果要使用单独的函数执行检查,则需要确保从函数中获取return值,因此这是函数的外观:

As mentioned in one of the comments on your question, you need to ensure you return a value from your function if you are using a separate function to perform the check, so this is how your function should look:

function checkPalindromes(string) {
  var items = string.toLowerCase().replace(/[^a-z0-9-\s]+/, '').split(' ');
  items.filter(function(item) {
    return item.split('').reverse().join('') === item;
  });

  return items;
}

您将使用以下命令调用它:

And you would call it using:

checkPalindromes('I live at home with my Mum, my Dad and my Brother!'); // ['i', 'mum', 'dad']

这篇关于字符串元素数组上的Javascript映射方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆