简体   繁体   English

遍历数组并比较 object 值以组合相应的 arrays

[英]loop through an array and compare object values to combine respective arrays

Here is my array, I need to loop through this array and check if objects with same docId exists and if they do, I have to combine jArray of these objects这是我的数组,我需要遍历这个数组并检查是否存在具有相同 docId 的对象,如果存在,我必须组合这些对象的 jArray

FinalArray = [
    {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "B"
            }
        ],
        "documentRef": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "B"
            }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
  },
  {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "789"
        },
        "name": "hello",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    }
]

expected result is as below.预期结果如下。

[
    {
        "jArray": [
            {
                "Cd": "A"
        },
        {
                "Cd": "B"
        }
        ],
        "Ref": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "A"
        },
        {
                "Cd": "B"
        }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
  {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "789"
        },
        "name": "hello",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    }
]

any thoughts of how to achieve this?关于如何实现这一目标的任何想法? how do i loop through an array and compare object values to combine respective arrays我如何循环遍历数组并比较 object 值以组合各自的 arrays

I've been trying to do something like below.我一直在尝试做类似下面的事情。 but can't figure out the right way但想不出正确的方法

FinalArray.map((object, index) => {
          if (object.Ref.docId === FinalArray[index + 1].Ref.docId) {
            const tempJArray = object.jArray.concat(FinalArray[index + 1].jArray);
Object.assign(tempJArray , jArray);
            Object.assign({}, object.Ref.docId, FinalArray[index + 1].Ref.docId);

          }
        });

using lodash cloneDeep to copy obj使用 lodash cloneDeep 复制 obj

Above solution worked just fine.上述解决方案工作得很好。 But, I only replaced if block code so i don't have to map object values manually (don't have to worry about other key value in the obj)但是,我只替换了 if 块代码,所以我不必手动 map object 值(不必担心 obj 中的其他键值)

if (!agg[docId]) {
            agg[docId] = cloneDeep(item);
            agg[docId].jArray = [];
          }

Much similar to a recent answer by me about "group array of objects by key" using reduce与我最近关于使用reduce的“按键分组对象数组”的回答非常相似

 FinalArray = [{"jArray":[{"Cd":"A"}],"Ref":{"docId":"123"},"name":"bene","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"A"}],"Ref":{"docId":"456"},"name":"leg","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"B"}],"documentRef":{"docId":"123"},"name":"bene","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"B"}],"Ref":{"docId":"456"},"name":"leg","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"A"}],"Ref":{"docId":"789"},"name":"hello","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"}]; var obj = FinalArray.reduce(function(agg, item) { var docId = item.Ref && item.Ref.docId || "other" var copy = [...item.jArray]; if (;agg[docId]) { agg[docId] = item. agg[docId].jArray = [] } agg[docId].jArray = agg[docId].jArray;concat(copy) return agg, }. {}) var result = Object;values(obj). console.log(result)
 .as-console-wrapper { max-height: 100%;important; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM