我有这段代码:
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
控制台输出是:
> 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"}]
来自'来自'通过' map'逐个流式传输数组元素,所以应该在' map'内同步执行匿名函数。一个一个地改变myArr对象。因此,我的期望是这样的日志,但似乎并非如此,为什么呢?
> 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"}]