3 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以獲取密鑰,然后使用它們?cè)L問(wèn)數(shù)組,并基于此進(jìn)行過(guò)濾:
const search = [2342,1900,1800,2310];
const result = Object.keys(search)
.filter(key => search[key] > 2200)
.map(key => Number(key))
console.dir(result)
或者,對(duì)于更有趣的事情:
const search = [2342,1900,1800,2310];
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
const filter = f => a => a.filter(f);
const map = f => a => a.map(f);
const toPairs = o => Object.keys(o)
.map(k => [k, o[k]]);
const take = i => a => a[i];
const gte = a => b => b >= a;
const toNumber = x => Number(x);
const bigEnough = gte(2200);
const itemOneIsBigEnough = pipe(
take(1),
bigEnough
);
const getTheFirstItemAsANumber = pipe(take(0), toNumber);
const doTheThing = pipe(
toPairs,
filter(
itemOneIsBigEnough
),
map(getTheFirstItemAsANumber),
);
const result = doTheThing(search);
console.dir(result);

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以獲取鍵并按值檢查過(guò)濾。
const
search = [2342, 1900, 1800, 2310],
indices = [...search.keys()].filter(i => search[i] > 2200);
console.log(indices);

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
最簡(jiǎn)單的方法是只是Array#reduce
項(xiàng)目:
const search = [2342,1900,1800,2310]
const result = search.reduce(function (result, el, index) {
if(el > 2200) {
result.push(index);
}
return result;
}, []);
console.log(result);
添加回答
舉報(bào)