根据输入数组处理复杂的对象 [英] Manipulating a complex object based on an input array

查看:76
本文介绍了根据输入数组处理复杂的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的对象,并且需要根据输入数组修改此复杂对象内部的属性.插图如下所示. 我需要根据字段值将它们分组,然后将它们添加到对象的或"数组(如果字段值相同).如果不相同,则将其直接添加到和"对象数组中. 需要从输入数组中读取每个字段,并检查该字段是否存在于"filterObj"的过滤器"中,如果存在,则将其添加至"and",否则将其添加至"or"

I have an complex object and based on an input array I need to modify properties inside of this complex object. Illustration is shown below. I need to group them based on field values and then add them to the "or" array of objects(if field value is same). If not same,directly add it to the "and" array of objects. Need to read each field from the input array,and check if this is present inside "filter" of "filterObj" and if present add it to "and" else add it to the "or"

两者的格式如下所示

//This is the source object where I need to add the below "arr" based on grouping

let filterObj = {
      "feature": "test",
      "filter": {
                 "and": [
                          { "field": "field1","value": "1"}
                        ]
                }
};

//This array needs to be added to above object by grouping based on field
let obj = [
             {"field" : "field1","value": "2"},
             {"field" : "field2","value": "3"},
             {"field" : "field2","value": "4"},
             {"field" : "field3","value" : "5"}
          ]


我希望输出具有以下格式:

I want the output to be of following format:

var result = {
              "feature": "test",
              "filter": {
                 "and": [
                          {
                           "or" : [
                                    {"field": "field1","value": "1"},
                                    {"field": "field1", "value": "2"}
                                  ]                      
                          },
                          {
                            "or" : [
                                     {"field": "field2","value": "3"},
                                     {"field": "field2","value": "4"},
                                   ]                      
                          },
                          {  "field": "field3", "value": "5"}
                  ]
              } 
}

// The method that I have tried

filterObj.filter.and.or(...obj) ;// Does not work 


推荐答案

您可以通过分组来收集所有相同的字段,并将分组中的值分配给and属性.

You could collect all same fields by a grouping and assign the values from grouping to and property.

var filterObj = { feature: "test", filter: { and: [{ field: "field1", value: "1" }] } },
    obj = [{ field: "field1", value: "2" }, { field: "field2", value: "3" }, { field: "field2", value: "4" }, { field: "field3", value: "5" }],
    groups = obj.reduce(
        (r, o) => {
            if (r[o.field]) {
                if (!('or' in r[o.field])) r[o.field] = { or: [r[o.field]] };
                r[o.field].or.push(o);
            } else {
                r[o.field] = o;
            }
            return r;
        },
        (filterObj.filter.and || []).reduce((r, o) => {
            r['or' in o ? o.or[0].field : o.field] = o;
            return r;
        }, {}));

filterObj.filter.and = Object.values(groups);

console.log(filterObj);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于根据输入数组处理复杂的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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