我有一个数组
var arr = ["abc=0","def=2","ghi=3","jkl=6","mno=9"];
现在我想检查数组中是否存在“ghi”和“mno”。如果那么价值是什么。
答案 0 :(得分:2)
这是怎么做的:
var arr = ["abc=0","def=2","ghi=3","jkl=6","mno=9"];
for(var child in arr){
console.log(arr[child].includes("mno"));
}

编辑,对于旧浏览器,您可以这样做:
var arr = ["abc=0","def=2","ghi=3","jkl=6","mno=9"];
for(var child in arr){
console.log(arr[child].search("mno"));
}

其中0等于真^^
答案 1 :(得分:2)
var arr = ["abc=0","def=2","ghi=3","jkl=6","mno=9"];
function valueOf (array, key) {
var value;
if (array.find(function (item) {
return ([ , value ] = item.split("="))[0] === key
})) {
return value;
}
}
console.log(valueOf(arr, "ghi")); // 3
console.log(valueOf(arr, "mno")); // 9
console.log(valueOf(arr, "foo")); // undefined
答案 2 :(得分:1)
使用lodash
var arr = ["abc=0","def=2","ghi=3","jkl=6","mno=9"];
var r = _.find(arr, function(i) { return i.includes('ghi'); });
alert(r);
答案 3 :(得分:1)
您可以使用Array.prototype.find()和String.prototype.includes()
arr.find(function(item) {
if(typeof item === "string" && item.includes("asd")) {
return true;
}
return false;
});
现在,如果找到了一个项目,你可以假设在数组中找到了该字符串。
答案 4 :(得分:1)
循环遍历数组并检查每个字符串。
sqlCopyTable(ch,
srctable = 'READ_ONLY.EXAMPLE',
desttable = 'WORK.EXAMPLE')
Error in if (as.character(keys[[4L]]) == colnames[i]) create <- paste(create, :
argument is of length zero
&#13;
function searchStringInArray (str, strArray) {
for (var j=0; j<strArray.length; j++) {
if (strArray[j].match(str)) return j;
}
return -1;
}
&#13;
答案 5 :(得分:1)
最好和最简单的逻辑是:
arr.find(function(item) {
return (typeof item === "string" && item.includes("ghi"));
});