JSFiddle - React, Tailwind, and code Playground
HTML
<button data-bind="click: remove">
Remove
</button>
JavaScript
var vm = function() {
var self = this;
self.customOptionVal = ko.observableArray([{
Color: [{
sub_color: "Red",
sub_id: "options_3_2",
is_checked: true
},
{
sub_color: "Green + $250.00",
sub_id: "options_3_3",
is_checked: true
}
],
Size: {
sub_size: "L",
sub_id: "options_2_2",
is_checked: true
}
}]);
self.remove = function(subId) {
subId = "options_3_2";
//Removes from the customOptionVal observable array
self.customOptionVal.remove(function(option) {
return ko.utils.arrayFilter(option.Color, function(color) {
return color.sub_id === subId;
});
});
console.log(self.customOptionVal());
self.customOptionVal.push({
Color: [{
sub_color: "Red",
sub_id: "options_3_2",
is_checked: true
},
{
sub_color: "Green + $250.00",
sub_id: "options_3_3",
is_checked: true
}
],
Size: {
sub_size: "L",
sub_id: "options_2_2",
is_checked: true
}
});
//Removes specific Color from the Color array
self.customOptionVal().forEach(function(option) {
var index = option["Color"].findIndex(function(y) {
return y.sub_id === subId;
});
if (index > -1) {
option["Color"].splice(index, 1);
}
});
console.log(self.customOptionVal());
};
}
ko.applyBindings(new vm());