Want to remove duplicate values from KoObservableArray. I have following JS file. I want to bind the name column in the UI as dropdown as well as to remove the duplicate values.
Suggest the best way to do the same.
JS File
$(function () {
var initialData = [
{ name: "Well-Travelled Kitten", sales: 352, price: 75.95 },
{ name: "Speedy Coyote", sales: 89, price: 190.00 },
{ name: "Furious Lizard", sales: 152, price: 25.00 },
{ name: "Furious Lizard", sales: 213, price: 25.00 },
{ name: "Indifferent Monkey", sales: 1, price: 99.95 },
{ name: "Speedy Coyote", sales: 89, price: 190.00 },
{ name: "Brooding Dragon", sales: 0, price: 6350 },
{ name: "Ingenious Tadpole", sales: 39450, price: 0.35 },
{ name: "Optimistic Snail", sales: 420, price: 1.50 }
];
var PagedGridModel = function (items) {
this.items = ko.observableArray(items);
};
ko.applyBindings(new PagedGridModel(initialData));
});
HTML File
<body>
<nav>
<label for="name">Name</label>
<select id="name" data-bind="options: items, optionsText: 'name', optionsCaption:'All'">
</select>
</nav>
</body>
The above give me drop down with duplicate names.
See below for a sample of how you can have a computed observable returning an array. I have just written down the logic for filtering but it may not be correct or optimal, I will leave it to you to improve on it.
And finally in your HTML