js 数组的indexOf可以判断对象数组中的对象有没有在数组中吗 js怎么判断某个对象是否在数组中

js \u6570\u7ec4\u7684indexOf\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u5bf9\u8c61\u6709\u6ca1\u6709\u5728\u6570\u7ec4\u4e2d\u5417

\u53ef\u4ee5\u3002
var o1 = { name: 'o1' };var o2 = { name: 'o2' };var o3 = { name: 'o3' };var arr = [ o1, o2, o3 ];arr.indexOf( o1 ); // 0arr.indexOf( o2 ); // 1arr.indexOf( o3 ); // 2arr.indexOf( o2 ) >= 0 // true

\u53ef\u4ee5\u4f7f\u7528indexof\u51fd\u6570
Definition and Usage
The indexOf() method searches the array for the specified item, and returns its position.
The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.
Returns -1 if the item is not found.

可以的,如:

var arr=[1,2,3];
arr.indexOf(4);//结果为-1,因为数组中不存在4
arr.indexOf(1);//结果为0,存在则返回内容在数组所在的索引


扩展阅读:javascript indexof ... js indexof函数 ... js lastindexof ... js数组indexof方法 ... indexof 用法js ... js splice ... js indexof方法错误返回 1 ... js substring ... js中indexof方法 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网