首页 > 解决方案 > 使用 lodash 在带有整数的数组中查找字符串元素包括不工作

问题描述

这是我的代码,我正在使用 vuejs:

pins: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], //my data

isThisPin(pin){
    return _.includes(this.pins, pin);
}

console.log(this.isThisPin('X')); //it returns true

基本上我试图猜测所选引脚是否不包含在引脚中,我希望将其返回为假,但它只是继续返回为真。

我使用了错误的功能吗?

标签: javascriptvue.jslodash

解决方案


如果你想使用 lodash,你可以使用 _.contains 片段也可以和 lodash 一起使用

_.contains([1, 2, 3], 'X') // false

var pins = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
//alert(_.includes(pins, 'X'));

function checkPin(pin){
  return _.includes(pins,pin )
}

alert(checkPin('X'));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>


推荐阅读