में एक लिंक्ड सूची को पुन: प्रयोज्य बनाना मेरे पास जावास्क्रिप्ट में एक लिंक की गई सूची है जिसे मुझे for of
लूप के साथ पुन: प्रयोज्य बनाने की आवश्यकता है। मैंने लगभग यह किया है लेकिन परिणाम में पहला मूल्य शामिल करने का कोई तरीका नहीं है।ईएस 6
var obj = {value: 1, next: {value: 2, next: {value: 3, next: {value: 4, next: {value: 5, next: {value: 6, next: {value:7, next: null}}}}}}};
obj[Symbol.iterator] = function() {
var current = this;
return {
next() {
if (current.next !== null) {
current = current.next;
return {value: current.value, done: false};
}
return {done: true}
}
}
}
for (const x of obj) {
console.log(x)
}
// this is how you get the values printed with no loop
// console.log(obj.value + '->' + obj.next.value + '->' + obj.next.next.value)