3 回答
TA贡献2039条经验 获得超7个赞
只需通过简单的方法即可。
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贡献1804条经验 获得超2个赞
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贡献2051条经验 获得超10个赞
您可以通过多种方式实现这一点。一种算法涉及将对象转换为数组。然后确定中点。
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)
}
})
添加回答
举报