淘汰赛:按特定字段值删除数组元素 [英] Knockout : Remove array element by specific field value

查看:76
本文介绍了淘汰赛:按特定字段值删除数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过特定的字段值删除ko可观察的数组元素.我尝试了一种解决方案.但是,缺少一些东西.它不起作用.

customOptionVal : ko.observableArray([])

customOptionVal是ko observableArray,其输出是:

Color: [0: {sub_color: "Red", sub_id: "options_3_2", is_checked: true}
1: {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}

现在,我希望这样,如果 sub_id = options_3_2 ,那么它将在 sub_id 的基础上从颜色元素中删除.

我尝试了以下解决方案.但是,它不起作用:

$.each(self.customOptionVal()['Color'], function( key, val ) {
                    if(self.customOptionVal()['Color'][key].sub_id == 'options_3_2') {
                        self.customOptionVal.remove(self.customOptionVal()['Color'][key]);
                    }
                });

解决方案

我发现了更好的方法:

如屏幕截图所示,创建一个ko可观察数组,并在该 ko.observableArray

中设置 Color

custom_option_select_text_arr = ko.observableArray([])
.....
this.custom_option_select_text_arr.push({sub_color: "Red", sub_id: "options_3_2", is_checked: true});
this.customOptionVal()['Color'] = this.custom_option_select_text_arr();

现在,对于删除元素:

self.custom_option_select_text_arr.remove(self.custom_option_select_text_arr()[0]);
self.customOptionVal()['Color'] = this.custom_option_select_text_arr();

I want to remove ko observable array element by specific field value. I tried one solution. But, there are something missing. It's not working.

customOptionVal : ko.observableArray([])

customOptionVal is ko observableArray and output of that is :

Color: [0: {sub_color: "Red", sub_id: "options_3_2", is_checked: true}
1: {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}

Now, I want like that if sub_id = options_3_2 then, it will remove from Color element on the base of sub_id.

I tried this below solution. But, it's not working :

$.each(self.customOptionVal()['Color'], function( key, val ) {
                    if(self.customOptionVal()['Color'][key].sub_id == 'options_3_2') {
                        self.customOptionVal.remove(self.customOptionVal()['Color'][key]);
                    }
                });

解决方案

I found better way that :

As see in screenshot, create one ko observable array and set Color value in that ko.observableArray

custom_option_select_text_arr = ko.observableArray([])
.....
this.custom_option_select_text_arr.push({sub_color: "Red", sub_id: "options_3_2", is_checked: true});
this.customOptionVal()['Color'] = this.custom_option_select_text_arr();

Now, for remove element :

self.custom_option_select_text_arr.remove(self.custom_option_select_text_arr()[0]);
self.customOptionVal()['Color'] = this.custom_option_select_text_arr();

这篇关于淘汰赛:按特定字段值删除数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆