Get index of array which containsinclude string value in javascriptNode.js - Stack Overflow

My question is how to get index of array which containsincludes string value. See my code below to get

My question is how to get index of array which contains/includes string value. See my code below to get the result I want.

This is simple code to get index of array:

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles"));
console.log(arraycontainsturtles) // the result will be 2

I want to get the index result with a code sample below:

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turt"));
console.log(arraycontainsturtles) // i want the result will be 2 with the contains string just the 'turt' only. 

How to get the index with a contains string value like at the sample number 2? The real result of the number 2 will be -1.

How do I do that?

My question is how to get index of array which contains/includes string value. See my code below to get the result I want.

This is simple code to get index of array:

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles"));
console.log(arraycontainsturtles) // the result will be 2

I want to get the index result with a code sample below:

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turt"));
console.log(arraycontainsturtles) // i want the result will be 2 with the contains string just the 'turt' only. 

How to get the index with a contains string value like at the sample number 2? The real result of the number 2 will be -1.

How do I do that?

Share Improve this question edited Apr 11, 2018 at 10:43 Didi Rumapea asked Apr 11, 2018 at 10:05 Didi RumapeaDidi Rumapea 551 gold badge1 silver badge6 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 1

Use findIndex instead of indexOf, as you can provide a function.

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.findIndex(function(item){
    return item.indexOf("turt")!==-1;
}));
console.log(arraycontainsturtles) // 2

var myarr = ["I", "like", "turtles"];
    var arraycontainsturtles = -1;
    for(i=0;i<myarr.length;i++){
    if((myarr[i].indexOf("turt")) != -1){
           arraycontainsturtles = i;
    }
    }
    console.log(arraycontainsturtles)

Hope this is what your looking for

var myarr = ["I", "like", "turtles"];
var search = "turt";
var arraycontainsturtles = myarr.reverse().reduce((a, v, i) => v.indexOf(search) != -1 ? Math.abs(i - myarr.length + 1) : a, -1);
console.log(arraycontainsturtles)

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = function(arr, str){
  let filter = -1;
  filter = arr.findIndex((e) => {
     return e.indexOf(str) > -1 ; 
  });
 return filter;
}

console.log(arraycontainsturtles(myarr, "turt")) 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625111a4636759.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信