javascript - Matching exact string with indexOf - Stack Overflow

Say I have something like this:[[ 'Friend' ],[ 'Friendship-' ],[ 'Friends&#

Say I have something like this:

[
[ 'Friend' ],
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'friendster' ],
]

I want to go through this and find the field that matches "Friend", but Friend only - not Friends, Friendship etc. How would I do this?

I have tried indexOf() & regex match, but I am still a beginner so it always matches them all right now.

Say I have something like this:

[
[ 'Friend' ],
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'friendster' ],
]

I want to go through this and find the field that matches "Friend", but Friend only - not Friends, Friendship etc. How would I do this?

I have tried indexOf() & regex match, but I am still a beginner so it always matches them all right now.

Share Improve this question edited Nov 15, 2018 at 19:51 R. Kohlisch asked Nov 15, 2018 at 18:35 R. KohlischR. Kohlisch 3,0137 gold badges33 silver badges64 bronze badges 2
  • What is that data structure you are starting with. Should that all be enclosed in a larger array that you want to filter? Is it a string? It's helpful if your examples can be copied and pasted without syntax errors. – Mark Commented Nov 15, 2018 at 18:37
  • 1 try regex ^[F|f]riend$ – Matt.G Commented Nov 15, 2018 at 18:38
Add a ment  | 

4 Answers 4

Reset to default 2

To find the index you can use findIndex()

If you might have more than one to find, you can use filter() which will return a list of matches. Since you want to match Friend and only Friend you can just use equality === to test.

Here's examples of both findIndex and filter():

let arr = [
    [ 'Friendship-' ],
    [ 'Friends' ],
    [ 'Friend' ],
    [ 'friendly' ],
    [ 'friendster', 'Friend' ]
]
// get first matching index
let idx = arr.findIndex(item => item.includes('Friend'))
console.log("Found Friend at index:", idx) // will be -1 if not found

// filter for `Friends`
let found = arr.filter(i => i.includes('Friend'))
console.log("All with friend: ", found)

There are a few good ways to solve this problem, since you mentioned RegExp This example will use a regular expression and the test method (which returns a boolean). the ^ at the head of the expression tells the evaluator to only look in cases where the line starts with the expression. The $ does the same for the end. Combined you get a line which exactly matches your search criteria.

const arr = [
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'Friend' ],
[ 'friendster' ],
]

const index = arr.findIndex(([val]) => /^Friend$/.test(val));

if (index === -1) {
  console.log('No Match Found');
  return;
}
console.log(arr[index]);

If your data structure is an array of arrays with a single item of a string, you could use filter:

let items = [
  ['Friend'],
  ['Friendship-'],
  ['Friends'],
  ['friendly'],
  ['friendster']
];

items = items.filter(x => x[0] === "Friend");
console.log(items);

let say you have

let a = [ [ 'Friend' ], [ 'Friendship-' ], [ 'Friends' ], [ 'friendly' ], [ 'friendster' ] ]

then:

let find = (a,w)=>{let i=-1; a.map((x,j)=>{if(x[0]===w) i=j}); return i}

let index = find(a, 'Friend'); // result: 0

if not found then find returns -1

UPDATE

And here is shorter version based on Mark Meyer answer:

var find = (a,w)=>a.findIndex(e=>e[0]===w);  // find(a, 'Friend') -> 0

let a = [ [ 'Friend' ], [ 'Friendship-' ], [ 'Friends' ], [ 'friendly' ], [ 'friendster' ] ]

var find = (a,w)=>a.findIndex(e=>e[0]===w); 


console.log( find(a, 'Friend') );

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

相关推荐

  • javascript - Matching exact string with indexOf - Stack Overflow

    Say I have something like this:[[ 'Friend' ],[ 'Friendship-' ],[ 'Friends&#

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信