/**
* 创建二维数组
*/
const nrows = 2;
const ncols = 4;
const matrix = Array.from({ length: nrows }, _ => new Array(ncols).fill(1));
console.log(matrix)
/**
* 拉平二维数组
*/
console.log([[1, 2, 3], [4, 5, 6]].flat().includes(1))
/**
* 手写flatten
*/
function flatten(arr) {
const isDeep = arr.some(item => Array.isArray(item));
if(!isDeep) return arr;
const re = Array.prototype.concat.apply([], arr);
return flatten(re);
}
console.log(flatten([[1,2],[3,4,[5,6,[7]]]]));
/**
* 数组去重
*/
function unique(arr) {
const set = new Set(arr);
return [...set];
}
console.log(unique([1,2,2,3,3,3,4,5]))
/**
* 判断空数组
*/
console.log([] === [])
/**
* 转换成String
*/
let i = new Array();
i = undefined;
// console.log(String(i))
// console.log(i.toString())
/**
* for in
* for of
*/
const arr = [1, 2, 3, { '4': '4' }, { '5': { 'nested': '5' } }]
for (let num in arr) {
// 打印的是属性名
console.log(num)
}
for (let num of arr) {
// 打印的是属性值
console.log(num)
}
console.log([10, 20, 30].map(parseInt))
console.log([10, 20, 30].map((item, index) => parseInt(item, index)))
console.log(parseInt(30, 2))
console.log(typeof [])
const n1 = undefined;
const n2 = null;
console.log(n1 == n2)
function max() {
return Math.max.apply(this, arguments);
}
console.log(max(1, 2, 3, 4, 5))
arr = []
console.log(Array.prototype.isPrototypeOf(arr))
console