这篇文章介绍3个用于检索数组内容的方法
一、keys()方法
keys()方法返回数组索引的迭代器
代码如下(示例):
const a=["red","blue","white"];
//通过Array.from()直接转换为数组实例
const akeys=Array.from(a.keys());
console.log(akeys); //[0,1,2]
二、values()方法
values()返回数组元素的迭代器
代码如下(示例):
const a=["red","blue","white"];
//通过Array.from()直接转换为数组实例
const avalues=Array.from(a.values());
console.log(avalues); //["red","blue","white"]
三、entries()方法
entries()返回索引/值对的迭代器
代码如下(示例):
const a=["red","blue","white"];
//通过Array.from()直接转换为数组实例
const aentries=Array.from(a.entries());
console.log(atries); //[[0,"red"],[1,"blue"],[2,"white"]]