最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS實(shí)現(xiàn)的Object數(shù)組去重功能示例【數(shù)組成員為Object對(duì)象】

 更新時(shí)間:2019年02月01日 14:45:21   作者:lmn13  
這篇文章主要介紹了JS實(shí)現(xiàn)的Object數(shù)組去重功能,可實(shí)現(xiàn)針對(duì)數(shù)組成員為Object對(duì)象的去重復(fù)功能,涉及javascript數(shù)組元素遍歷、屬性判斷等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)的Object數(shù)組去重功能。分享給大家供大家參考,具體如下:

目標(biāo):實(shí)現(xiàn)成員為 Object 的數(shù)組的去重。

注意,這里的數(shù)組成員為 Object,而不是數(shù)值或者字符串。

調(diào)用方法:

arr = distinct_arr_element(arr);

函數(shù):

/*
 * 在數(shù)組中去除重復(fù)項(xiàng)()
 */
var distinct_arr_element = function( arr ){
  if( !arr ) return null ;
  var resultArr = [];
  $(arr).each( function( index, el ){
    var notExist = true ;
    $(resultArr).each( function(i,element){
      if( isObjectValueEqual( el, element ) ){
        notExist = false ;
        return false ;
      }
    });
    if( notExist )
      resultArr.push( el );
  });
  return resultArr ;
}
/*
 * 判斷兩個(gè) Object 的值是否相等
 */
function isObjectValueEqual(a, b) {
  // Of course, we can do it use for in Create arrays of property names
  var aProps = Object.getOwnPropertyNames(a);
  var bProps = Object.getOwnPropertyNames(b);
  // If number of properties is different, objects are not equivalent
  if (aProps.length != bProps.length) {
    return false;
  }
  for ( var i = 0; i < aProps.length; i++ ) {
    var propName = aProps[i];
    // If values of same property are not equal, objects are not equivalent
    if (a[propName] !== b[propName]) {
      return false;
    }
  }
  // If we made it this far, objects are considered equivalent
  return true;
}

完整測(cè)試示例如下:

<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
<script>
/*
 * 在數(shù)組中去除重復(fù)項(xiàng)()
 */
var distinct_arr_element = function( arr ){
  if( !arr ) return null ;
  var resultArr = [];
  $(arr).each( function( index, el ){
    var notExist = true ;
    $(resultArr).each( function(i,element){
      if( isObjectValueEqual( el, element ) ){
        notExist = false ;
        return false ;
      }
    });
    if( notExist ) 
      resultArr.push( el );
  });
  return resultArr ;
}
/*
 * 判斷兩個(gè) Object 的值是否相等
 */
function isObjectValueEqual(a, b) {
  // Of course, we can do it use for in Create arrays of property names
  var aProps = Object.getOwnPropertyNames(a);
  var bProps = Object.getOwnPropertyNames(b);
  // If number of properties is different, objects are not equivalent
  if (aProps.length != bProps.length) {
    return false;
  }
  for ( var i = 0; i < aProps.length; i++ ) {
    var propName = aProps[i];
    // If values of same property are not equal, objects are not equivalent
    if (a[propName] !== b[propName]) {
      return false;
    }
  }
  // If we made it this far, objects are considered equivalent
  return true;
}
var arrDemo=[{'name':'jb51.net'},{'name':'jb51.net'},{'age':10},{'age':12}];
console.log(distinct_arr_element(arrDemo))
</script>

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行結(jié)果:

PS:這里再為大家提供幾款相關(guān)工具供大家參考使用:

在線去除重復(fù)項(xiàng)工具:
http://tools.jb51.net/code/quchong

在線文本去重復(fù)工具:
http://tools.jb51.net/aideddesign/txt_quchong

更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript字符與字符串操作技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

措勤县| 安化县| 山阳县| 广河县| 景宁| 乐业县| 颍上县| 伊川县| 大同市| 屏东市| 海宁市| 长沙市| 花莲市| 嘉鱼县| 宁强县| 长乐市| 张家口市| 郸城县| 雅江县| 玉田县| 多伦县| 崇文区| 襄汾县| 灵山县| 卢氏县| 温州市| 昔阳县| 阿合奇县| 洱源县| 平顺县| 庆阳市| 浠水县| 开封县| 冀州市| 哈密市| 中牟县| 久治县| 缙云县| 天镇县| 泽州县| 南木林县|