我是javascript新手,正在尝试理解代码中的原因
var result1 =(['001', '005', '007', '009'], '007');
console.log(result1.indexOf('007'));
我可以使007工作,因为它是嵌套数组的最高级别,但是将console.log(result1.indexOf('007'));
更改为console.log(result1[0].indexOf('007'));
或console.log(result1.indexOf(['001', '005', '007', '009']));
仅返回-1。
任何人都可以帮助说明我如何格式化该方法或为什么它不适用于嵌套对象吗?
简短说明对于indexOf()
let arrays = [1, 7, 2, 9, 10, 89, 12]
let isExist = arrays.indexOf(89) // it will provide give you the position of the value in the arrays
console.log(isExist) // 5
/* Suppose the value is not in the array then it will return -1 i.e value doesn't exist in the array*/
isExist = arrays.indexOf(21) // it will provide give you the position of the value in the arrays
console.log(isExist) // -1
InShort indexOf()
用于检查值在数组中是否存在。