JavaScript findIndex
返回第一个找到的索引(如果值重复)
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const index = arr.findIndex(m => m.a === 10)
console.log(index);
上面的代码只会返回0索引。
我该怎么做才能获得索引2。
答案 0 :(得分:2)
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const indices = [...arr.keys()].filter(i => arr[i].a === 10)
console.log(indices)
或者,只需使用for
循环
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const output = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].a === 10)
output.push(i)
}
console.log(output)
答案 1 :(得分:0)
arr.reduce((result, current, index) => {
return result.concat(current.a == 10 ? [index]: [])
})
答案 2 :(得分:0)
您可以使用Array.reduce
循环访问arr并返回每个索引
单行:
const arr = [{
a: 10,
b: 20,
c: 30,
}, {
a: 15,
b: 25,
c: 32,
}, {
a: 10,
b: 23,
c: 350,
}]
// Value to check for
const v = 10;
// we loop on every entry of the arr
// we start with an empty array and look if the entries
// contains the value 10, if they do, we push the index of the
// entry in the array that was empty
//
// we do that for every entry of arr, when we treat all, we return
// the list of matching entries
const idx = arr.reduce((tmp, x, xi) => (x.a === v ? [...tmp, xi] : tmp), []);
console.log(idx);
解释:
const arr = [{
a: 10,
b: 20,
c: 30,
}, {
a: 15,
b: 25,
c: 32,
}, {
a: 10,
b: 23,
c: 350,
}]
const valToCheck = 10;
const indexes = arr.reduce((tmp, x, xi) => {
if (x.a === valToCheck) {
tmp.push(xi);
}
return tmp;
}, []);
console.log(indexes);
答案 3 :(得分:0)
使用map
和filter
:
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}];
const res = arr.map(({ a }, i) => a == 10 ? i : "").filter(String);
console.log(res);
答案 4 :(得分:0)
您可以映射找到的项目的索引并仅过滤有效的索引。
const
array = [{ a: 10, b: 20, c: 30 }, { a: 15, b: 25, c: 32 }, { a: 10, b: 23, c: 350 }],
indices = array
.map((m, i) => m.a === 10 ? i : -1)
.filter(i => i != -1);
console.log(indices);
答案 5 :(得分:0)
您可以遍历数组并将匹配索引推入新数组中。
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
let p = []
arr.forEach((ele,index)=> ele.a===10 ? p.push(index) : '')
console.log(p)
答案 6 :(得分:0)
您可以尝试这样:
<Button
Panel.ZIndex="100"
BorderThickness="0"
BorderBrush="Transparent"
Background="Black"
Grid.Column="1"
VerticalAlignment="Stretch"
Margin="0 2 0 5"
Command="{Binding DataContext.TestRadTreeCommand, RelativeSource={RelativeSource AncestorType=TreeView}}">
<Image Width="20"
Height="20"
Source="/CCRIS;component/Resources/Images/03-Add.png"/>
</Button>
答案 7 :(得分:0)
如果您想要直接与Array一起使用的函数,请将其添加到Array.prototype
Array.prototype.findAllIndexes = function(iteratee){
var resultArray = [];
this.forEach(function(element) {
var validated = iteratee.call(this, element);
if(validated) {
resultArray.push(element);
}
});
return resultArray;
}
然后您可以将其与数组对象一起使用
(请根据所使用的od数据类型处理极端情况)
const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const index = arr.findAllIndexes(m => m.a === 10)
console.log(index);
您可以使用一些帮助程序库来处理诸如
lodash.js
之类的东西,您可以将它们包含在项目中。