数组的实例---数组的entries(),keys(),和values() ,includes()
2018-03-09 16:32
316 查看
ES6 提供三个新的方法——
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"如果不使用
let entries = letter.entries();
console.log(entries.next().value); // [0, 'a']
console.log(entries.next().value); // [1, 'b']
console.log(entries.next().value); // [2, 'c']数组实例的includes
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true该方法的第二个参数表示搜索的起始位置,默认为
[1, 2, 3].includes(3, -1); // true没有该方法之前,我们通常使用数组的
// ...
}
// -1
// true下面代码用来检查当前环境是否支持该方法,如果不支持,部署一个简易的替代版本。const contains = (() =>
Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
)();
contains(['foo', 'bar'], 'baz'); // => false另外Map,Set数据结构有一个has方法,需要注意与includes()的区分Map 结构的has方法,是用来查找键名的,比如Map.prototype.has(key)、WeakMap.prototype.has(key)、
entries(),
keys()和
values()——用于遍历数组。它们都返回一个遍历器对象(详见《Iterator》一章),可以用
for...of循环进行遍历,唯一的区别是
keys()是对键名的遍历、
values()是对键值的遍历,
entries()是对键值对的遍历。for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"如果不使用
for...of循环,可以手动调用遍历器对象的
next方法,进行遍历。let letter = ['a', 'b', 'c'];
let entries = letter.entries();
console.log(entries.next().value); // [0, 'a']
console.log(entries.next().value); // [1, 'b']
console.log(entries.next().value); // [2, 'c']数组实例的includes
Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的
includes方法类似。ES2016 引入了该方法。
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true该方法的第二个参数表示搜索的起始位置,默认为
0。如果第二个参数为负数,则表示倒数的位置,如果这时它大于数组长度(比如第二个参数为
-4,但数组长度为
3),则会重置为从
0开始。[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true没有该方法之前,我们通常使用数组的
indexOf方法,检查是否包含某个值。if (arr.indexOf(el) !== -1) {
// ...
}
indexOf方法有两个缺点,一是不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于
-1,表达起来不够直观。二是,它内部使用严格相等运算符(
===)进行判断,这会导致对
NaN的误判。[NaN].indexOf(NaN)
// -1
includes使用的是不一样的判断算法,就没有这个问题。[NaN].includes(NaN)
// true下面代码用来检查当前环境是否支持该方法,如果不支持,部署一个简易的替代版本。const contains = (() =>
Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
)();
contains(['foo', 'bar'], 'baz'); // => false另外Map,Set数据结构有一个has方法,需要注意与includes()的区分Map 结构的has方法,是用来查找键名的,比如Map.prototype.has(key)、WeakMap.prototype.has(key)、
Reflect.has(target, propertyKey)。
Set 结构的has方法,是用来查找值的,比如Set.prototype.has(value)、WeakSet.prototype.has(value)。
相关文章推荐
- es6 数组实例的 entries() , keys() 和 values()
- 数组实例的entries(),keys() 和 values()方法
- ES6-数组的扩展-数组实例的entries(),keys()和values()
- ES6的数组扩展( entries(),keys(),values() )
- keys,values,entries(获取对象或数组的值或索引)
- php array_values 返回数组的值实例详解
- 2016/3/20 数组定义 数组遍历 超全局数组 数组元素设置(in_array() 、array_reverse()、count()、array_unique()、unset()、array_values、array_merge、array_push) 列表实例
- [js高手之路] es6系列教程 - 迭代器,生成器,for...of,entries,values,keys等详解
- ES6-对象的扩展-Object.keys(),Object.values(),Object.entries()
- 4000 es6笔记·····entries(),keys(),values()
- php array_values 返回数组的所有值详解及实例
- php array_values 返回数组的值实例详解
- 数组实例的includes()方法
- es7 数组实例的 includes()
- array_keys && array_values::PHP数组键名于键值分离
- 数组实例的includes()
- php array_values 返回数组的所有值详解及实例
- es6 Object.keys(),Object.values(),Object.entries()
- ES6-数组的扩展-数组实例的includes()
- NSDictionary allkeys或者allvalues取出来的数组是乱序的。