I have this code:
let myArr = [{a: 'Apple'}, {a: 'Banana'}, {a: 'Carrot'}];
let stream = Observable
.from(myArr)
.map( el => {
el.a = 'Coke';
console.log(`interim----- ${el} ||| ${myArr}`);
return el;
});
stream.subscribe( elm = > elm )); // to trigger the stream
The console output is:
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Coke"}, {a: "Coke"}]
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Coke"}, {a: "Coke"}]
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Coke"}, {a: "Coke"}]
As 'from' streams the array elements one-by-one via 'map', so should the synchronous execution of the anonymous function inside the 'map' alter the myArr objects one-by-one. Therefore my expectation was for a log like this, but it seems that's not the case, why actually?
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Banana"}, {a: "Carrot"}] // diff
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Coke"}, {a: "Carrot"}] // diff
> interim----- {a: "Coke"} ||| [{a: "Coke"}, {a: "Coke"}, {a: "Coke"}]