怎样的indexOf的JavaScript用绳子和阵列工作方式不同? [英] How javascript indexOf works differently with string and array?

查看:157
本文介绍了怎样的indexOf的JavaScript用绳子和阵列工作方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串和字符串数组。

I have both string and array of strings.

var strFruit = "Apple is good for health";  
var arrayFruit = ["Apple is good for health"]; 

var strResult = strFruit.indexOf("Apple"); //strResult shows 0  
var arrayResult =  arrayFruit.indexOf("Apple"); // arrayResult  shows -1  

但是,如果使用 arrayFruit.indexOf(苹果是有益身体健康) arrayResult显示为0。

But if i use arrayFruit.indexOf("Apple is good for health") arrayResult shows 0.

我的问题是的为什么的indexOf会在数组元素完全一致,但不是在字符串并搜索如何两者相差?

And my Question is why indexOf looks for exact match in Array element but not in string and how both search differ?.

的jsfiddle

PS :主要原因问这个问题是我无法理解的源$ C ​​$ C为的indexOf 。我能理解<强>什么它(的indexOf ),用绳子和array.But林不知道如何,它正在用绳子和Array?( <一href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Description\"相对=nofollow>聚满了,或者源$ C ​​$ C )。

P.S: Main reason to ask this question is i am unable to understand source code for indexOf.I can understand what it does(indexOf) with string and array.But im not sure how it is working with string and Array?(poly fills or source code).

推荐答案

strFruit.indexOf(苹果)字符串搜索苹果 strFruit ,但 arrayFruit.indexOf(苹果)搜索项数组与值苹果

strFruit.indexOf("Apple") searches for string Apple in strFruit but arrayFruit.indexOf("Apple") searches for an item in array with the value Apple

这篇关于怎样的indexOf的JavaScript用绳子和阵列工作方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆