/** * 對於由簡單類型數據組成的對象為元素組成的數組進行去重操作 * @params {Array} 需要去重的對象數組 * @returns {Array} 去重後的對象數組 */ function distinct(sourceArray) { var resultArray = []; v ...
1 /** 2 * 對於由簡單類型數據組成的對象為元素組成的數組進行去重操作 3 * @params {Array} 需要去重的對象數組 4 * @returns {Array} 去重後的對象數組 5 */ 6 function distinct(sourceArray) { 7 8 var resultArray = []; 9 var i, j, currentSource, currentResult; 10 11 for (i = 0; i < sourceArray.length; i++) { 12 13 currentSource = sourceArray[i]; 14 15 if (resultArray.length === 0) { 16 resultArray.push(currentSource); 17 continue; 18 } 19 20 for (j = 0; j < resultArray.length; j++) { 21 22 currentResult = resultArray[j]; 23 24 if (!compare(currentResult, currentSource)) { 25 resultArray.push(currentSource); 26 } 27 28 } 29 30 } 31 32 return resultArray; 33 34 function compare(obj1, obj2) { 35 for (var prop in obj1) { 36 37 if (!obj1.hasOwnProperty(prop)) { 38 continue; 39 } 40 41 if (obj1[prop] !== obj2[prop]) { 42 return false; 43 } 44 45 } 46 47 return true; 48 } 49 50 }
/** * 對於由簡單類型數據組成的對象為元素組成的數組進行去重操作 * @params {Array} 需要去重的對象數組 * @returns {Array} 去重後的對象數組 */ function distinct(sourceArray) {
var resultArray = []; var i, j, currentSource, currentResult;
for (i = 0; i < sourceArray.length; i++) {
currentSource = sourceArray[i];
if (resultArray.length === 0) { resultArray.push(currentSource); continue; }
for (j = 0; j < resultArray.length; j++) {
currentResult = resultArray[j];
if (!compare(currentResult, currentSource)) { resultArray.push(currentSource); }
}
}
return resultArray;
function compare(obj1, obj2) { for (var prop in obj1) {
if (!obj1.hasOwnProperty(prop)) { continue; }
if (obj1[prop] !== obj2[prop]) { return false; }
}
return true; }
}