I'm trying to update UI when array propety is updated ,
I have array of colors , I trying to update the color red in colors array but it doesnot update the UI
var ViewModel = function() {
this.self = this;
self.colors = ko.observableArray([
{ color: "red", test: "aaa" },
{ color: "blue", test: "bbb" },
{ color: "yellow", test: "ccc" }
]);
self.replaceIt = function() {
const result = self.colors().filter(c => c.color === "red");
result[0].color = "green";
for (var i = 0; i < self.colors().length; i++) {
self.colors.replace(self.colors()[0], result[0]);
}
};
};
ko.applyBindings(new ViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<ul data-bind="foreach: colors">
<li>
<span data-bind="text:color"></span>
<span data-bind="text:test"></span>
</li>
</ul>
<br/>
<br/>
<button data-bind="click:replaceIt">Replace It</button>
the result UI always display
red aaa
blue bbb
yellow ccc
but I want to update UI when I update property red to green
green aaa
blue bbb
yellow ccc