2017-02-08 153 views
2

我有以下樣本陣列(原始陣列具有多於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" 
    ] 
    } 
] 

條件=如果(客戶端ID & &祖先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代碼來實現這一點。

+0

你的榜樣合併兩個條目具有不同的ID。 – epiqueras

+0

@ user3742114謝謝 – user3742114

+0

在分組結果中,不同'accesstoken'會發生什麼? –

回答

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; 
})); 
} 
+0

艾哈邁德..你可以提供for循環也..我無法得到所需的輸出..我更新了我的帖子中的數組.. – user3742114

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);

+0

嘿謝謝..我在node.js中工作,我得到這個錯誤=> array.reduce((newArray,entry)=> { ^ SyntaxError:意外的令牌> – user3742114

+0

@ user3742114現在應該被修復,請嘗試運行代碼片段 – epiqueras

0

一個可以處理這個辦法是遍歷您的數組並將結果添加到基於合併條件的唯一組合的Map中。類似於ID的連接應該起作用。

如果地圖在此條件下已經有一個對象作爲其關鍵字,我們將當前項目與該值合併,否則我們將其添加。

您的代碼的一個瓶頸是,您還需要對這些對象進行深度合併,這些對象在JavaScript中本地不受支持,超出了此問題的範圍。您可以檢查出this SO thread

下面是一個例子(我使用deepmerge庫合併):如果需要

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);
<script src="https://unpkg.com/[email protected]/index.js"></script>

ES5版本:

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);
<script src="https://unpkg.com/[email protected]/index.js"></script>

+0

我是在節點中得到這個錯誤。js => const mergeKey =({clientid,ancestorid})=>'$ {clientid} $ {ancestorid}'; ^ SyntaxError:意外的令牌, – user3742114

+0

@ user3742114這可能是因爲您的節點版本不支持[ES6解構](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/ Destructuring_assignment)。我也增加了一個ES5版本。 – nem035

0

使用簡單的Javascript,您可以使用散列表作爲關閉與clientidancestorid分組。

stroyid針對唯一值進行過濾。

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

非常感謝你..它的工作..還有一件事,是否有可能刪除故事ID中的重複值? – user3742114