2017-02-08 1 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" 
    ] 
    } 
] 

条件= IF(クライアント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を持つ2つのエントリをマージ。 – 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

Ahmad .. 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

配列を作成し、マージ条件のユニークな組み合わせに基づいてマップに結果を追加します。 IDの連結のような何かが働くはずです。

マップにこの条件のオブジェクトがキーとして既に存在する場合は、現在のアイテムを値とマージし、それ以外の場合は追加します。

コードのボトルネックの1つは、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 destructuring]をサポートしていない可能性があります(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

本当にありがとうございました..もう1つ、ストーリーIDの重複値を削除することは可能ですか? – user3742114

関連する問題