目录
前言
自己做的一个js相关题目整理。
一、数组扁平化
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
数组扁平化是指将一个多维数组转变为一个一纬数组
1.使用flat()
flat(depth),depth参数可选填,默认为1,指嵌套函数应该展平的深度的深度级别
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
// expected output: [0, 1, 2, 3, 4]
const arr2 = [0, 1, 2, [[[3, 4]]]];
console.log(arr2.flat(2));
// expected output: [0, 1, 2, [3, 4]]
const arr3 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr3.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2.利用正则
const stt = [1,[2,[3,[4,5]]],6];
const res2 = JSON.stringify(stt).replace(/\[|\]/g,'').split(',');
注意:数据类型会变为字符串