如何根据条件比较数组元素和合并?

时间:2017-02-08 16:54:17

标签: javascript arrays node.js

我有以下示例数组。(原始数组有超过200个元素)

var array = [
  {
    "clientid": "ID000002",
    "accesstoken": "pllALEl3TlwLL9hHP938H",
    "groupname": "ABC",
    "ancestorid": "8982857550",
    "stroyid": [
      "IGN-EXM-001-PDF",
      "IGN-EXM-002-PDF"
    ]
  }, {
    "clientid": "ID000002",
    "accesstoken": "pllALEl3TlwpHOD4aTP38H",
    "groupname": "EFG",
    "ancestorid": "4705872914",
    "stroyid": [
      "APP-ENE-FIE-CON",
      "APP-ENE-ASS-INS",
      "APP-ENE-ASS-CAR",
      "APP-ENE-MAT-REA"
    ]
  }, {
    "clientid": "ID000002",
    "accesstoken": "pllALEl3TlwLL9hHP938H",
    "groupname": "ABC",
    "ancestorid": "8982857550",
    "stroyid": [
      "IGN-EXM-001-ZIP",
      "IGN-EXM-002-ZIP"
    ]
  }
]

条件= if(客户端id&& ancestor id相同然后合并storyid)所以输出应该是这样的:

[{
  "clientid": "ID000002",
  "accesstoken": "pllALEl3TlwLL9hHP938H",
  "groupname": "ABC",
  "ancestorid": "8982857550",
  "stroyid": [
    "IGN-EXM-001-PDF",
    "IGN-EXM-002-PDF",
    "IGN-EXM-001-ZIP",
    "IGN-EXM-002-ZIP"

  ]
}, {
  "clientid": "ID000002",
  "accesstoken": "pllALEl3TlwpHOD4aTP38H",
  "groupname": "EFG",
  "ancestorid": "4705872914",
  "stroyid": [
    "APP-ENE-FIE-CON",
    "APP-ENE-ASS-INS",
    "APP-ENE-ASS-CAR",
    "APP-ENE-MAT-REA"
  ]
}]

请帮助我使用javascript代码来实现此目的。

4 个答案:

答案 0 :(得分:0)

您可以使用以下方法合并storyId而不重复,然后将其替换为原始的storyId数组:

if(array[0].clientid == array[1].clientid && array[0].ancestorid == array[1].ancestorid){
var c = array[0].stroyid.concat(array[1].stroyid.filter(function (item) {
    return array[0].stroyid.indexOf(item) < 0;
}));
}

答案 1 :(得分:0)

const myArray = [
  { clientid: 1, ancestorid: 1, stroyid: ['a', 'b', 'c'] },
  { clientid: 1, ancestorid: 1, stroyid: ['a', 'b', 'd', 'e'] },
];

const newArray = myArray.reduce((newArray, entry) => {
  // Find if there is a duplicate in the new array
  const duplicate = newArray.find(newEntry => (
    entry.clientid === newEntry.clientid && entry.ancestorid === newEntry.ancestorid
  ));

  // If there is a duplicate, merge their stroyids
  if (duplicate) {
    const cleanIDs = entry.stroyid.filter(id => !duplicate.stroyid.includes(id));
    duplicate.stroyid = duplicate.stroyid.concat(cleanIDs);
  }
  else newArray.push(entry); // Else, add the entire entry

  return newArray;
}, []);

console.log(newArray);

答案 2 :(得分:0)

您可以采用的一种方法是迭代数组,并根据合并条件的唯一组合将结果添加到Map。像ids的连接之类的东西应该有效。

如果地图已经将此条件下的对象作为其键,我们将当前项与值合并,否则,我们将其添加。

您的代码的一个瓶颈是您还需要深度合并这些对象,这些对象在JavaScript中本身不受支持,并且超出了此问题的范围。您可以查看this SO thread

以下是一个示例(我使用deepmerge库进行合并):

&#13;
&#13;
var array=[{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP938H",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-PDF","IGN-EXM-002-PDF"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwpHOD4aTP38H",groupname:"EFG",ancestorid:"4705872914",stroyid:["APP-ENE-FIE-CON","APP-ENE-ASS-INS","APP-ENE-ASS-CAR","APP-ENE-MAT-REA"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP9n",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-ZIP","IGN-EXM-002-ZIP"]}];
    
const map = new Map();
const mergeKey = ({ clientid, ancestorid }) => `${clientid}${ancestorid}`;

for (const item of array) {
  const key = mergeKey(item);
  if (map.has(key)) {
    map.set(key, deepmerge(map.get(key), item));
  } else {
    map.set(key, item);
  }
}

const merged = Array.from(map.values());
console.log(merged);
&#13;
<script src="https://unpkg.com/deepmerge@1.3.2/index.js"></script>
&#13;
&#13;
&#13;

ES5版本(如果需要):

&#13;
&#13;
var array=[{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP938H",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-PDF","IGN-EXM-002-PDF"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwpHOD4aTP38H",groupname:"EFG",ancestorid:"4705872914",stroyid:["APP-ENE-FIE-CON","APP-ENE-ASS-INS","APP-ENE-ASS-CAR","APP-ENE-MAT-REA"]},{clientid:"ID000002",accesstoken:"pllALEl3TlwLL9hHP9n",groupname:"ABC",ancestorid:"8982857550",stroyid:["IGN-EXM-001-ZIP","IGN-EXM-002-ZIP"]}];
    
var map = Object.create(null);
function mergeKey(item) {
  return item.clientid + item.ancestorid;
}

array.forEach(function(item) {
  var key = mergeKey(item);
  if (map[key]) {
    map[key] = deepmerge(map[key], item);
  } else {
    map[key] = item;
  }
});

var merged = Object.keys(map).map(function(key) {
  return map[key];
});
console.log(merged);
&#13;
<script src="https://unpkg.com/deepmerge@1.3.2/index.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

使用普通的Javascript,您可以使用哈希表作为关闭,以便使用clientidancestorid进行分组。

stroyid会针对唯一值进行过滤。

&#13;
&#13;
var array = [{ clientid: "ID000002", accesstoken: "pllALEl3TlwLL9hHP938H", groupname: "ABC", ancestorid: "8982857550", stroyid: ["IGN-EXM-001-PDF", "IGN-EXM-002-PDF", "dupe"] }, { clientid: "ID000002", accesstoken: "pllALEl3TlwpHOD4aTP38H", groupname: "EFG", ancestorid: "4705872914", stroyid: ["APP-ENE-FIE-CON", "APP-ENE-ASS-INS", "APP-ENE-ASS-CAR", "APP-ENE-MAT-REA"] }, { clientid: "ID000002", accesstoken: "pllALEl3TlwLL9hHP9n", groupname: "ABC", ancestorid: "8982857550", stroyid: ["IGN-EXM-001-ZIP", "IGN-EXM-002-ZIP", "dupe"] }],
    result = array.reduce(function (hash) {
        return function (r, a) {
            var key = [a.clientid, a.ancestorid].join('|');
            if (!hash[key]) {
                hash[key] = { clientid: a.clientid, accesstoken: a.accesstoken, groupname: a.groupname, ancestorid: a.ancestorid, stroyid: a.stroyid };
                return r.concat(hash[key]);
            }
            hash[key].stroyid = hash[key].stroyid.concat(a.stroyid).filter(function (temp) {
                return function (a) {
                    return !temp[a] && (temp[a] = true);
                };
            }(Object.create(null)));
            return r;
        };
    }(Object.create(null)), []);
    
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;