YUI.Array.dedupe函数,如果传参为有length属性,返回一个去除掉重复项('1’ 与1 | true 与'true’认为相等)的参数数组副本,如果传参的length为undefined,那么返回一个空数组[],如果传参没length属性,抛出一个类型错误

数组去重YArray.dedupe = Lang._isNative(Object.create) ? function (array) {
var hash = Object.create(null),
results = [],
i, item, len; for (i = 0, len = array.length; i < len; ++i) {
item = array[i]; if (!hash[item]) {
hash[item] = 1;
results.push(item);
}
} return results;
} : function (array) {
var hash = {},
results = [],
i, item, len; for (i = 0, len = array.length; i < len; ++i) {
item = array[i]; if (!hasOwn.call(hash, item)) {
hash[item] = 1;
results.push(item);
}
} return results;
};

1.  在一个对象obj中 obj[1] === obj['1’]    obj[true] === obj[‘true’]

2.  这个函数不会去比较数据类型,如果一个值在空对象中没值,则赋值为1,并将它push进数组,下次有相同的值,直接略过

3.  在现代浏览器中,可以直接调用Obejct.create(),构建一个没有原型的对象,但在ie8及以下中,直接创建一个{},每次还要用hasOwnProperty去检测是否是{}的属性,再去执行2的操作,速度减慢

对象var y = YUI()
var newArr = y.Array
arr = [1,2,3,4,5,1,'1',true.'true']
str = '12345671'
obj = {name: 'fsy'}
arr1 = newArr.dedupe(arr) // ===> [1,2,3,4,5,true]
arr2 = newArr.dedupe(str) // ====> [“1”,”2”,”3”,”4”,”5”,”6”,”7”]
arr3 = newArr.dedupe(obj) // ====> []

YUI Array 之dedupe(快速去重)的更多相关文章

  1. js Array 交集 并集 差集 去重

    最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上,网上的实现方式都是2次循环,性能不适合我的需求,1000*1000那循环次数太多了,所以我这里采用对象object来 ...

  2. 用几条shell命令快速去重10G数据

    试想一下,如果有10G数据,或者更多:怎么才能够快速地去重呢?你会说将数据导入到数据库(mysql等)进行去重,或者用java写个程序进行去重,或者用Hadoop进行处理.如果是大量的数据要写入数据库 ...

  3. YUI Array 之some(检测|any)

    YUI原码 YUI someYArray.some = Lang._isNative(Native.some) ? function (array, fn, thisObj) { return Nat ...

  4. YUI Array 之 indexOf(索引值|搜索)

    YUI原码 YUI indexOfYArray.indexOf = Lang._isNative(Native.indexOf) ? function (array, value, from) { r ...

  5. YUI Array 之hash

    hash就是把两个参数合并成一个类似hashMap结构的对象,用第一个数组的元素为key,第二个的为value,如果第二个参数未指定,则把对象的对应的值置为true YUI原码 YUI hashYAr ...

  6. YUI Array 之each| forEach(遍历)

    1. yui-each原码: 遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn ...

  7. php 数组元素快速去重

    1.使用array_unique方法进行去重 对数组元素进行去重,我们一般会使用array_unique方法,使用这个方法可以把数组中的元素去重. <?php $arr = array(,,,, ...

  8. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. 【转】 js数组 Array 交集 并集 差集 去重

    原文:http://blog.csdn.net/ma_jiang/article/details/52672762 最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上, ...

随机推荐

  1. Qt中gb2312/GBK的URL编解码函数

    编码函数: QByteArray encodeURI(QString str) { QByteArray array; QTextCodec *codec=QTextCodec::codecForNa ...

  2. mysql grant all privileges on

    遇到了 SQLException: access denied for @'localhost' (using password: no) 解决办法 grant all privileges on * ...

  3. dobbo学习

    http://www.cnblogs.com/Javame/p/3632473.html 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以 ...

  4. python视频教程大全集下载

    python3英文视频教程(全87集) http://pan.baidu.com/s/1dDnGBvV Python从入门到精通视频(全60集)链接:http://pan.baidu.com/s/1e ...

  5. PHP Warning: phpinfo(): It is not safe to rely on the system's timezone setting

    错误描述: PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *re ...

  6. 接入SDK

    管理提醒: 本帖被 fm2010 设置为精华(2014-11-12) http://www.cocoachina.com/bbs/read.php?tid-239087.html     本帖属于Co ...

  7. C语言实现OOP 版本3 :简化代码

    我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试 shape.h #ifndef SHAPE_H #define SHAPE_H typedef struct shape_t ...

  8. Effective Java2读书笔记-类和接口(二)

    第15条:使可变性最小化 通过一个复数类来看不可变类. public final class Complex { private final double re; private final doub ...

  9. 安装Oracle10g on RedHat as 4 64bit(摘)

    一.安装前的配置 1.修改RH版本 vi /etc/redhat-release Red Hat Enterprise Linux AS release 3 (Taroon Update 3) 2. ...

  10. 【转】如何在ubuntu12.04设置adb驱动

    原文网址:http://www.xuebuyuan.com/1475698.html 在ubuntu上adb驱动不用像在windows上一样需要额外装,只需要写一个配置文件就可以,下面是设置的步骤: ...