JS iterators only know one direction: forwards. Meaning, if you would want to use your custom iterator with a for-loop or the like, it would ignore your custom properties. You could however create your own custom iterator for your own use only:
const obj = {
values: [0,1,2,3],
[Symbol.iterator]() {
const values = this.values;
const min = 0;
const max = values.length - 1;
let index = -1;
return {
next() {
index = Math.min(index + 1, max);
return { value: values[index], done: index >= max };
},
prev() {
index = Math.max(index - 1, min);
return { value: values[index], done: index <= min };
}
}
}
}
var it = obj[Symbol.iterator]();
console.log(it.next().value); // 0
console.log(it.next().value); // 1
console.log(it.prev().value); // would be awesome if this was 0
console.log(it.prev().value); // would be awesome if this was 0
console.log(it.prev().value); // would be awesome if this was 0
console.log(it.next().value); // would be awesome if this was 1