JS 获取当前元素在数组中的索引 获取当前元素索引

1,519 阅读1分钟

1.JS 获取当前元素在数组中的索引

let arr = [{name: '测试'},{name: '假如'}]

如: data.parentAttrName = '测试'

let index = arr.map(item => item.name).indexOf(data.parentAttrName)

console.log(index)   // 输出 0

2.获取当前元素索引

let arr = ["'sheng", "shi", "qu", "Jiedao"]

如: attr.name = 'sheng'

let currentAttrIndex = sequence.indexOf(attr.name)

console.log(currentAttrIndex)   // 输出 0