如何在数组中查找重复项并合并嵌套数据

时间:2018-03-14 18:59:14

标签: javascript lodash

我有一个带有重复项的对象数组。对象根级别的数据是相同的,但嵌套的对象数组则不相同。这是我需要在运行lodash uniqBy之前进行合并或者删除重复项的任何函数。

这是具有重复项的对象数组。

    [
    {
        "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
        "name": "College Park / Brookhaven",
        "mktId": 0,
        "status": "Unknown",
        "code": "197D6",
        "ownershipType": null,
        "series": [
            {
                "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
                "name": "02,03",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
        "name": "College Park / Hillsdale",
        "mktId": 0,
        "status": "Unknown",
        "code": "295D6",
        "ownershipType": null,
        "series": [
            {
                "id": "807144A1-26ED-4657-9775-7DF7563107D3",
                "name": "02",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    }
]

大学公园/汉普郡在这个例子中是重复的。我需要找到2个重复项并返回此预期结果..

   [
    {
        "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
        "name": "College Park / Brookhaven",
        "mktId": 0,
        "status": "Unknown",
        "code": "197D6",
        "ownershipType": null,
        "series": [
            {
                "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
                "name": "02,03",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            },
            {
                "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
        "name": "College Park / Hillsdale",
        "mktId": 0,
        "status": "Unknown",
        "code": "295D6",
        "ownershipType": null,
        "series": [
            {
                "id": "807144A1-26ED-4657-9775-7DF7563107D3",
                "name": "02",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    }
]

vanilla javascript或lodash很好。

2 个答案:

答案 0 :(得分:1)

我使用HashMap创建了reduce,在该hashMap中,键是项的名称,值是项本身。

const data = [{
    "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
    "name": "College Park / Brookhaven",
    "mktId": 0,
    "status": "Unknown",
    "code": "197D6",
    "ownershipType": null,
    "series": [{
      "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
      "name": "01",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
    "name": "College Park / Hampshire",
    "mktId": 0,
    "status": "Unknown",
    "code": "316D6",
    "ownershipType": null,
    "series": [{
      "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
      "name": "02,03",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
    "name": "College Park / Hampshire",
    "mktId": 0,
    "status": "Unknown",
    "code": "316D6",
    "ownershipType": null,
    "series": [{
      "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
      "name": "01",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
    "name": "College Park / Hillsdale",
    "mktId": 0,
    "status": "Unknown",
    "code": "295D6",
    "ownershipType": null,
    "series": [{
      "id": "807144A1-26ED-4657-9775-7DF7563107D3",
      "name": "02",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  }
];

const hashMap = data.reduce((hash, item) => {
  const key = item.name;
  if (hash.hasOwnProperty(key)) {
    hash[key].series = hash[key].series.concat(item.series);
  } else {
    hash[key] = item;
  }
  return hash;
}, {});

const result = Object.keys(hashMap).map((key) => hashMap[key]);

console.log(result);

答案 1 :(得分:0)

只需为id和结果数组设置一个hashmap:

set.seed(100)
    library(caTools)
    library(caret)
    library(e1071)
    folds<-createFolds(wpdc$outcome, k=10) 
    CV <- lapply(folds, function(x){
      traing_folds=wpdc[-x,]
      test_folds=wpdc[x,]
      dataset_model_nb<-naiveBayes(outcome ~ ., data = traing_folds)
      dataset_predict_nB<-predict(dataset_model_nb, test_folds[-1])
      dataset_table_nB<-table(test_folds[,1],dataset_predict_nB)
      accuracy<-confusionMatrix(dataset_table_nB, positive ="R")
      return(accuracy)
    })

 outcome radius_mean texture_mean perimeter_mean area_mean smoothness_mean compactness_mean concavity_mean concave_points_mean symmetry_mean fractal_dimension_mean radius_se texture_se perimeter_se area_se smoothness_se
1       N       18.02        27.60         117.50    1013.0         0.09489           0.1036         0.1086             0.07055        0.1865                0.06333    0.6249     1.8900        3.972   71.55      0.004433
2       N       17.99        10.38         122.80    1001.0         0.11840           0.2776         0.3001             0.14710        0.2419                0.07871    1.0950     0.9053        8.589  153.40      0.006399
3       N       21.37        17.44         137.50    1373.0         0.08836           0.1189         0.1255             0.08180        0.2333                0.06010    0.5854     0.6105        3.928   82.15      0.006167

然后迭代数组:

const hash = {}, result = [];

现在我们可以检查id是否已经出现在哈希中,如果是,只需添加系列:

 for(const el of array){
   const { id, series } = el;

如果id尚未出现,我们需要将对象添加到结果和哈希表中:

   if(hash[id]){
     hash[id].series.push(...series);
   } else {

那已经是它了:))