使用lodash _.forEach遍历动态数组

时间:2018-12-26 11:20:29

标签: javascript laravel vue.js lodash

我想通过lodash填充一个动态嵌套数组,它会根据vue.js中我选择输入字段的no:of改变

例如,对于一个类别,它可能具有3个选择输入字段(大小,颜色,材料)和2个输入字段(大小,材料)用于另一个类别。选择输入字段的no:of在类别之间更改。

(data is bind to this.temp) temp: {
                color: [],
                size: {
                    parent: '',
                    child: []
                },
                material: []
            },
productAttribute: function () {
                this.form.test_product_attributes = [];
                _.forEach(this.available_fields, function (attr_value, key) {
                    let loop_array = (attr_value.is_has_child !== null) ? this.temp[key].child : this.temp[key];
                    if(loop_array.length != 0 ){
                        _.forEach(loop_array, function (temp_value, temp_key) {
                            let obj = {
                                is_available: true,
                                qty: 0,
                                price: 0,
                            };
                            obj[key] = (attr_value.is_has_child !== null) ? _.find(attr_value.options[this.temp[key].parent], ['id', temp_value]) : _.find(attr_value.options, ['id', temp_value]);
                            this.form.test_product_attributes.push(obj);
                            for(let i in  this.form.test_product_attributes){
                                if(!this.form.test_product_attributes[i][key]){
                                    this.form.test_product_attributes[i][key] =  (attr_value.is_has_child !== null) ? _.find(attr_value.options[this.temp[key].parent], ['id', temp_value]) : _.find(attr_value.options, ['id', temp_value]);
                                }
                            }
                        }.bind(this));
                    }
                }.bind(this));
            },

我想检索一个包含每个选择结果的对象数组,

[{"is_available":true,"color":{"id":1,"name":"Red"},"size":{"id":5,"name":"16","size_by":"Full Look"},"qty":0,"price":0},{"is_available":true,"color":{"id":1,"name":"Red"},"size":{"id":6,"name":"17","size_by":"Full Look"},"qty":0,"price":0},{"is_available":true,"color":{"id":2,"name":"Green"},"size":{"id":5,"name":"16","size_by":"Full Look"},"qty":0,"price":0},{"is_available":true,"color":{"id":2,"name":"Green"},"size":{"id":6,"name":"17","size_by":"Full Look"},"qty":0,"price":0}]

 this.available_fields:{"size":{"field_id":39,"field_name":"Size","product_attributes":1,"value_type":null,"fieldinputtype":"DropdownList","is_has_child":43,"form_name":"size","options":{"CM":[{"id":1,"name":"100"},{"id":2,"name":"101"},{"id":3,"name":"102"}],"Full Look":[{"id":4,"name":"15.5"},{"id":5,"name":"16"},{"id":6,"name":"17"},{"id":7,"name":"18"},{"id":8,"name":"19"},{"id":9,"name":"20"}]},"value":{"parent":"","child":""}},"color":{"field_id":38,"field_name":"Color","product_attributes":1,"value_type":"array","fieldinputtype":"DropdownList","is_has_child":null,"form_name":"color","options":[{"id":1,"name":"Red"},{"id":2,"name":"Green"},{"id":3,"name":"Yellow"}],"value":[]},"material":{"field_id":40,"field_name":"Material","product_attributes":null,"value_type":null,"fieldinputtype":"DropdownList","is_has_child":null,"form_name":"material","options":[{"id":1,"name":"leather"},{"id":2,"name":"nylone"}],"value":""}}

0 个答案:

没有答案