js 数据结构 【】 map Set

js 对空位的处理,已经很不一致了,大多数情况下会忽略空位。

  • forEach(), arr.filter((item,index,o)=>{}), arr.reduce(ffunction add(){}), every() 和some()都会跳过空位。
  • arr.filter((item,index,o)=>{})   arr.map((item,index,o)=>{})
  • arr.reduce(ffunction (t,item,index,o){},0)   数组中的每个值(从左到右)开始缩减,最终计算为一个值。
  • map()会跳过空位,但会保留这个值
  • join()和toString()会将空位视为undefined,而undefined和null会被处理成空字符串。
  • Array.from方法会将数组的空位,转为undefined,也就是说,这个方法不会忽略空位。
  • entries()、keys()、values()、find()和findIndex()会将空位处理成undefined。
//1.扩展运算符(spread)是三个点(...)
console.log(1, ...[2, 3, 4], 5);
function push(array, ...items) {
  array.push(...items);
}
// 数组复制
const a1 = [1, 2];
const a2 = a1.concat();
const a3 = [...a1];
const [...a4] = a1;

// 数组合并
var arr1 = ['a', 'b'];
var arr2 = ['c'];
var arr3 = ['d', 'e'];
// ES6的合并数组
console.log([...arr1, ...arr2, ...arr3]); // [ 'a', 'b', 'c', 'd', 'e' ]

// arr.push()  扩展
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);


// 数组拆分【生成】
const [first, ...rest] = [1, 2, 3, 4, 5];
console.log(first) // 1
console.log(rest)  // [2, 3, 4, 5]
// 字符串转数组
console.log([...'hello']) // [ "h", "e", "l", "l", "o" ]


// 提取键值
let map = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
]);
let arr = [...map.keys()]; // [1, 2, 3]
let arr2 = [...map.values()]; // [1, 2, 3]


// 数组转换
let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from('hello')// ['h', 'e', 'l', 'l', 'o']
Array.from([1, 2, 3], (x) => x * x)// [1, 4, 9]


Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1

/*
target(必需):从该位置开始替换数据。如果为负值,表示倒数。
start(可选):从该位置开始读取数据,默认为 0。如果为负值,表示倒数。
end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。

*/

// -2相当于3号位,-1相当于4号位
[1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4, 2, 3, 4, 5]


[NaN].indexOf(NaN) // -1
[NaN].findIndex(item => Object.is(NaN, item))// 0


[1, 2, 3].includes(4)     // false
[1, 2, NaN].includes(NaN) // true

//Map 结构的has方法,是用来查找键名的

 


参考链接

https://blog.csdn.net/qq_24892029/article/details/79215133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值