这段代码不是很理解,求帮忙科普下。

这段代码不是很理解,求大神帮忙科普下。主要是2个for循环中的代码有点没看懂,方便的话给加上注释解释下。

     var array =  ['c', 'a', 'z', 'a', 'x', 'a'];

            function clear() {

                var o = {};//字面量方式创建个空对象

                for(var i = 0; i < array.length; i++) {

                    var item = array[i];//这段及以下代码怎么理解?

                    if(o[item]) {

                        o[item]++;

                    } else {

                        o[item] = 1;

                    }

                }//end for

                var tmpArray = [];

                for(var key in o) {

                    if(o[key] == 1) {

                        tmpArray.push(key);

                    } else {

                        if(tmpArray.indexOf(key) == -1) {

                            tmpArray.push(key);

                        }

                    }

                }//end for

                return tmpArray;

            }//end function

            

            console.log(clear(array));


翻阅古今
浏览 569回答 5
5回答

元芳怎么了

本段代码是通过Object key值的唯一性进行数组去重,然而这种方法仅仅对字符串数组去重有效,因为最终它是取对象的key值push进tmpArray,而对象的key值是字符串类型。如果需要去重的数组是[1, 1, 4, 8, 10, 8],根据此方法最终得到的结果是['1', '4', '8', '10'],改变了数据的类型,所以在不知道数组具体数据的类型时,不建议使用。推荐一种数组去重的简单方法:var array = ['c', 'a', 'z', 'a', 'x', 'a'];var newArr = [...new Set(array)];

阿波罗的战车

var array =&nbsp; ['c', 'a', 'z', 'a', 'x', 'a'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function clear() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var o = {};//字面量方式创建个空对象&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0; i < array.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var item = array[i];//这段及以下代码怎么理解?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(item,o[item],o)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(o[item]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o[item]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o[item] = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }//end for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tmpArray = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var key in o) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(o[key] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpArray.push(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(tmpArray.indexOf(key) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpArray.push(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }//end for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return tmpArray;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }//end function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(clear(array));通过Object key 唯一性去除数组中重复的值,代码质量不高,不看也罢

子衿沉夜

1、第一个for循环:创建对象存储数组元素出现次数,采用键值对表示2、第二个for循环:对创建后的对象遍历,如果值为1,说明他在原数组中只出现一次并存入新数组中,如果值不为1并且新数组中不存在则这个值也存入新数组中后面返回的就是去重后的数组

Cats萌萌

你把console.log(o)写在//end for后面,就懂了

慕森王

这代码真的是奇葩!楼上说的用set当然是可以的。var newArr = [...new Set(array)];如果你要自己实现也非常简单var array =&nbsp; ['c', 'a', 'z', 'a', 'x', 'a'];function distinct(arr) {&nbsp; &nbsp; const ret = {}&nbsp; &nbsp; arr.forEach(item => ret[item] = true);&nbsp; &nbsp; return Object.keys(ret)}distinct(array) // ['c', 'a', 'z', 'x']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript