Say I want to replace an array of objects with an array of decorators:
var arr = [{id:1, text: "text1"}, {id:2, text: "text2"}, {id:3, text: "text3"}];
arr.forEach(function(el, idx){
var newEl = new NewEl(el.id, el.text, arr[idx + 1], idx);
arr[idx] = newEl;
}, this);
console.log(arr);
function NewEl(id, text, nextEl, idx){
this.id = id;
this.text = text;
this.next = nextEl;
}
nextEl will still refer to the old object, not the next item in the array (whatever that element).
How do I pass a reference to the next element (location) in the array to the constructor function?
Note: If possible, I'd prefer not to use a workaround by modifying the logic within the loop (workaround involving setting 'next' on the previous element on next iteration).
Is it possible?