3 回答

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
只需通過簡單的方法即可。
let obj = {
1: 'one',
2: 'two',
3: 'three',
7: 'seven'
};
// make sure that your case is in order
// get keys of your object
const keys = Object.keys(obj);
// find index of starting key
const index = keys.indexOf('3');
// make sure index >= 0
// each all keys from starting key to the end by old school way
for (let i = index; i < keys.length; i++) {
var key = keys[i];
console.log(obj[key]);
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
continue如果鍵小于 ,您可以使用跳過迭代3:
let obj = {
1: 'one',
2: 'two',
3: 'three',
7: 'seven'
};
for(let [key, val] of Object.entries(obj)){
if(key < 3 ){
continue;
}
console.log(val);
}

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以通過多種方式實(shí)現(xiàn)這一點(diǎn)。一種算法涉及將對(duì)象轉(zhuǎn)換為數(shù)組。然后確定中點(diǎn)。
let obj = {
1: 'one',
2: 'two',
3: 'three',
7: 'seven'
}
Object.entries(obj).forEach(([key, value], index, array) => {
const split = array.length/2
const midPoint = split % 2 == 0 ? split : Math.floor(split)
if(index >= midPoint){
console.log(key)
}
})
添加回答
舉報(bào)