您如何使用indexOf来标识嵌套数组中的值

问题描述 投票:-1回答:1

我是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。

任何人都可以帮助说明我如何格式化该方法或为什么它不适用于嵌套对象吗?

javascript arrays nested indexof
1个回答
0
投票

简短说明对于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()用于检查值在数组中是否存在。

indexOf for array of object

JavaScript Array indexOf() Method

© www.soinside.com 2019 - 2024. All rights reserved.