gpt4 book ai didi

javascript - 我如何迭代(对于 obj 中的键)起始中间位置

转载 作者:行者123 更新时间:2023-11-30 19:18:33 25 4
gpt4 key购买 nike

我有一个对象:

let obj = {
1: 'one',
2: 'two',
3: 'three',
7: 'seven'
}

是否有机会从 3 位置开始迭代它,如下所示:

for (let key = 3 in obj) {
console.log(obj[key]) // An output will be 'three' and 'seven'
}

我需要以最快的方式做到这一点,因为一个对象非常大

最佳答案

用一种简单的方法来做。

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]);
}

关于javascript - 我如何迭代(对于 obj 中的键)起始中间位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com