Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is nicely namespaced and self-contained solution in code. It uses no "modern" (and possibly unavailable) features like <code>forEach</code> and <code>filter</code>. It cleans up the invalidated records after all is said and done, instead of going about deleting items i the live array while the process is going on. It is also very easy to modify if you want to change the way you identify duplicates or how to merge objects. </p> <p>There is a commented version (with example and a minified version) at pastie, <a href="http://pastie.org/private/1biqtnc9yvfribq9g1ka" rel="nofollow noreferrer">here</a>. </p> <pre><code>(function (global, ns) { function mergeRecordsAndCleanUp(arr) { var rec, dupeIndices, foundDupes; for(var idx=0, len=arr.length; idx&lt;len-1; ++idx) { rec = arr[idx]; if (rec===null) continue; dupeIndices = findDupeIndices(rec, arr.slice(idx+1), idx+1); if (dupeIndices.length===0) continue; foundDupes = true; processDupes(rec, dupeIndices, arr); } if (foundDupes) cleanUpArray(arr); } function cleanUpArray(arr) { for (var idx=0, len=arr.length; idx&lt;len; ++idx) { if (arr[idx]===null) arr.splice(idx--, 1); } } function processDupes(rec, dupeIndices, arr) { var dupeRealIdx, dupeRec; for (var dupeIdx=0, dupesLen=dupeIndices.length; dupeIdx&lt;dupesLen; ++dupeIdx) { dupeRealIdx = dupeIndices[dupeIdx]; dupeRec = arr[dupeRealIdx]; updateRecord(rec, dupeRec); arr[dupeRealIdx] = null; } } function findDupeIndices(rec, arr, offset) { var other, result = []; for (var idx=0, len=arr.length; idx&lt;len; ++idx) { other = arr[idx]; if (other===null) continue; if (isDupe(rec, other)) result.push(idx+offset); } return result; } function identicalArrays(arr0, arr1) { if (arr0.length!==arr1.length) return false; for (var idx=0, len=arr0.length; idx&lt;len; ++idx) { if (arr0[idx]!==arr1[idx]) return false; } return true; } function isDupe(original_record, candidate_record) { var orec_bits = original_record.encounter_bits , crec_bits = candidate_record.encounter_bits; return (crec_bits.min_level===orec_bits.min_level &amp;&amp; crec_bits.max_level===orec_bits.max_level) &amp;&amp; (identicalArrays(crec_bits.conditions, orec_bits.conditions)); } function updateRecord(rec, dupe) {rec.encounter_bits.rarity += dupe.encounter_bits.rarity} global[ns] = { npup: mergeRecordsAndCleanUp }; })(this, 'npup'/* sweet namespace eh */); // npup.npup(arr); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload