一、数组
const arr = [ ‘数’ , ‘组’]
1. 判断是否为数组
Array.isArray(arr)
console.log('判断arr是否为数组:' + Array.isArray(arr)); // 输出结果----判断arr是否为数组:true
console.log('判断[1,2]是否为数组:' + Array.isArray([1, 2])); // 输出结果----判断[1,2]是否为数组:true
console.log('判断空数组是否为数组:' + Array.isArray([])); // 输出结果----判断空数组是否为数组:true
console.log('判断数字是否为数组:' + Array.isArray(100)); // 输出结果----判断数字是否为数组:false
console.log('判断字符串是否为数组:' + Array.isArray('数组')); // 输出结果----判断字符串是否为数组:false
console.log('判断undefined是否为数组:' + Array.isArray(undefined)); // 输出结果----判断undefined是否为数组:false
console.log('判断null是否为数组:' + Array.isArray(null)); // 输出结果----判断null是否为数组:false
console.log('判断布尔值是否为数组:' + Array.isArray(true)); // 输出结果----判断布尔值是否为数组:false
2. 获取数组最后一个元素(获取元素)
arr.at(-1)
console.log('获取数组最后一个元素:' + arr.at(-1)); // 输出结果----获取数组最后一个元素:组
3.遍历数组元素
- forEach()
// 无返回值
arr.forEach((item, index) => {
index = index + 1
console.log('输出第' + index + '项:' + item);
});
// 输出结果
输出第1项:数
输出第2项:组
- map()
// 返回新的数组
const arr2 = arr.map(item => {
newItem = item + '+'
return newItem;
});
console.log(arr2);
// 输出结果
[ '数+', '组+' ]
- filter()
// 返回符合条件的数据项组成的新数组
const arr2 = arr.filter(item => {
return item !== '数';
});
console.log(arr2);
// 输出结果
[ '组' ]
- every()
// 只要有一个是false,便返回false
const result1 = arr.every(item => {
return item === '数'
});
console.log(result1);
// 输出结果
false
- some()
// 只要有一个是true,便返回true
const result = arr.some(item => {
return item === '数'
});
console.log(result);
// 输出结果
true
二、对象
const obj = { name: ‘张三’ }
1. 判断对象里是否含有某个属性
console.log('对象obj是否有属性name:' + Reflect.has(obj, 'name'));
console.log('对象obj是否有属性age:' + Reflect.has(obj, 'age'));
// 输出结果
对象obj是否有属性name:true
对象obj是否有属性age:false
三、缓存
1. localStorage
- localStorage.setItem(‘name’ , value);
- localStorage.getItem(‘name’)
- localStorage.removeItem(‘name’)
- localStorage.clear(‘name’)