简体   繁体   English

JavaScript-使用数组检查字符串

[英]JavaScript - Check string with an array

This piece of code should return hits = ["Heather, "Heather", "Heather"] but it doesn't. I'm not quite sure what I am doing wrong. 这段代码应该返回hits = [“” Heather,“ Heather”,“ Heather”],但不是。我不太确定自己在做什么错。

var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather", "fjdiafjdoisfhoids"];
var myName = "Heather";
var hits = [];
for(var i = 0; i < text.length; i++) {
    if (text[i] === myName[i]) {
        for(var j = i; j < (myName.length + i); j++); {
            hits.push(text[j]);
        }
    }
}

You can use array filter function: 您可以使用数组filter功能:

var hits = text.filter(function (obj) {
  return obj === myName;
});

Snippet 片段

 var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather", "fjdiafjdoisfhoids"]; var myName = "Heather"; var hits = text.filter(function (obj) { return obj === myName; }); console.log(hits); 

Obligatory use this library answer. 必须使用此库答案。 Here I use lodash: 在这里我使用lodash:

var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather", "something"],
    myName = "Heather";

var hits = _.filter(text, _.matches([myName]);

Explanation on what's happening because this isn't intuitive at all. 解释发生的事情,因为这根本不直观。 _.filter does the same thing as Array.prototype.filter except it's faster. _.filter与Array.prototype.filter具有相同的功能,只是速度更快。 You can run your own tests below to get an idea: 您可以在下面运行自己的测试来了解一下:

https://jsperf.com/array-filter-vs-lodash-filter-without-bias/2 https://jsperf.com/array-filter-vs-lodash-filter-without-bias/2

The explanation I've heard is that traditional array operations implemented by lodash (map, reduce, filter, etc.) don't have to follow the implementation as detailed by the spec. 我听到的解释是,由lodash实现的传统数组操作(映射,缩小,过滤器等)不必遵循规范详述的实现。

Anyways, _.filter takes two arguments: a collection (array or object) and function which returns true or false. 无论如何, _.filter接受两个参数:集合(数组或对象)和返回true或false的函数。 If true, it'll return the item. 如果为true,则将返回该项目。

_.matches is a convenience function which returns a function that takes an argument and matches against the contents of the provided collection. _.matches是一个便捷函数,它返回一个接受参数并与所提供集合的内容匹配的函数。

You could write the above another way: 您可以用另一种方式编写以上代码:

var hits = _.filter(text, function(str) {
  return str === myName
});

In lodash, we create functions all the time that basically executes an equality check. 在lodash中,我们始终创建基本上执行相等性检查的函数。 _.matches is just a convenience function to create equality check function. _.matches只是创建相等性检查功能的便利功能。

Check against myName variable. 检查myName变量。

var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather", 
                                        "fsomething"];
var myName = "Heather";
var hits = [];

for(var i = 0; i < text.length; i++) {
    if (text[i] === myName) {      
            hits.push(text[i]);      
    }
}
console.log(hits);

Here is a working sample 是一个工作样本

text[i] will never === myName[i] , because text is an array of strings, whereas myName is just one string. text[i]永远不会=== myName[i] ,因为text是一个字符串数组,而myName只是一个字符串。 So for example, text[1] === "Heather" , whereas myName[1] === "H" . 因此,例如, text[1] === "Heather" ,而myName[1] === "H" Any index of myName will just return one character. myName任何索引将只返回一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM