当前位置:网站首页>Array de duplication array sorting maximum sum class array conversion filter array
Array de duplication array sorting maximum sum class array conversion filter array
2022-07-19 12:27:00 【Starry dream】
One 、 Flatten nested arrays / Flattening and arraying holes ——flat()
1、 Realization effect
var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(2); // [1, 2, 3, 4, 5, 6] //flat Method to delete the empty slot in the array : var arr3 = [1, 2, , 4, 5]; arr3.flat(); // [1, 2, 4, 5]
The flat([depth]) Method to create a new array , All subarray elements are recursively connected to a specified depth .depth The depth level specifies how deep the nested array structure should be flattened . The default is 1.
2、 Reading, :
The essence of this method is to use recursion and array merging method concat Make it flat . have access to
Array.reduce()/Array.some()/Array.concat()
utilize Array.reduce() and Array.concat() Realization
const flat1 = function (arr) {
return arr = arr.reduce((pre, cur) => Array.isArray(cur) ? pre.concat(flat1(cur)) : pre.concat(cur),
[])
}
var arr3 = [1, 2, 3, [1, 2, 3, 4, [2, 3, 4]]];
flat1(arr3); // [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
utilize Array.some() and Array.concat() Realization
const flat2 = function(arr){
while(arr.some(item=>Array.isArray(item))){
arr=[].concat(...arr);
}
return arr;
}
var arr3 = [1, 2, 3, [1, 2, 3, 4, [2, 3, 4]]];
flat2(arr3); // [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Two 、 Array weight removal ——set()
1、 Realization effect
Array.from(new Set([1,2,3,3,4,4])) //[1,2,3,4] [...new Set([1,2,3,3,4,4,5,6])] //[1,2,3,4,5]
set yes ES6 A new type of data that defines non repeating arrays .
Array.from Is to convert a class array to an array .
... Is the extension operator , take set The values in it are converted to strings .
2、 Reading,
function unique(arr) {
var result =[];
for (var i = 0; i < arr.length; i++) {
for (var j = i+1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
j=++i;
}
}
result.push(arr[i]);
}
return result;
}
var arr3 = [1, 2, 3, 3, 4, 5, 4];
unique(arr3);//(5) [1, 2, 3, 5, 4]
3、 ... and 、 Array sorting ——sort()
1、 Realization effect
[1,3,2,5,4].sort(); [1,3,2,5,4].sort((a,b)=>b-a);
2、 Reading,
Bubble sort
Array.prototype.bulesort = function () {
let arr = this,
len = arr.length;
for (let outer = len; outer >= 2; outer--) {
for (let inner = 0; inner <= outer - 1; inner++) {
if (arr[inner] > arr[inner + 1]) {
[arr[inner], arr[inner + 1]] = [arr[inner + 1], arr[inner]];
}
}
}
return arr;
}
[3, 4, 2,1].bulesort()//[1,2,3,4]
Four 、 Maximum
1、 Realization effect
Math.max(...[1,2,3,4]); //4
Math.max.apply(this, [1, 2, 3, 4]); //4
[1, 2,3, 4].reduce((prev,cur)=>{
return Math.max(prev, cur);
},0);//4
2、 Reading,
Prioritize , Then take .
5、 ... and 、 Sum up
1、 Realization effect
[1, 2, 3, 4].reduce((prev,cur)=>{
return prev+cur;
},0); //10
2、 Reading,
function sum(arr){
if (arr.length==0){
return 0;
} else if (arr.length == 1){
return arr[0];
}else{
return arr[0]+sum(arr.slice(1));
}
}
sum([1, 2, 3, 4]);//10
6、 ... and 、 Merge
[1,2,3].concat([5,6]); [...[1,2,4],...[2,3,5,7]]
7、 ... and 、 Judge whether there is value
[2, 3, 5].indexOf(3);//1
[2,3,5].includes(4)//false
[2, 3, 5].find((item) => { item === 3})//1
[2, 3, 5].findIndex((item) => item === 3)//1
[2, 3, 5].some(item=>{return item===3 })//true
8、 ... and 、 Class array conversion
Array.prototype.slice.call(arguments); Array.prototype.slice.apply(arguments); Array.from(arguments); [...arguments]
Nine 、 Each setting value
//array.fill(value, start, end) [2, 3, 5].fill(true,false,true);//(3) [false, false, false] [2, 3, 5].map(() => 0)//(3) [0, 0, 0]
Ten 、 Whether each item satisfies / Whether a certain item satisfies
[2, 3, 5].every(item=>{return item>2}) //false
[2, 3, 5].some(item =>{ return item > 2 })//true
11、 ... and 、 Filter array
[2, 3, 5].filter(item => { return item > 2 })//(2) [3, 5]
Twelve 、 Object and array conversion
Object.keys({ name: ' Zhang San ', age: 14 })//["name", "age"]
Object.values({name: ' Zhang San ', age: 14 })// [" Zhang San ", 14]
Object.entries({ name: ' Zhang San ', age:14 }) // (2) [Array(2), Array(2)]0: (2)["name", " Zhang San "]1: (2)["age", 14]
Good use of , Let's collect it first
边栏推荐
- The leader of the new generation of cloud database -- AWS
- Microcomputer principle and technical interface experiment five basic IO operation temperature control experiment
- Opencv tutorial 03: how to track an object in a video
- 2022年低压电工考试题及在线模拟考试
- OpenCV 教程 03: 如何跟踪视频中的某一对象
- Leetcode 150. 逆波兰表达式求值
- Linux下MySQL的安装与使用
- psd. JS parsing PSD file
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- 一个技巧;教你轻松下载抖音直播视频,抖音直播视频下载新方案!
猜你喜欢

机器学习(上)吴恩达

Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1

字符串相关函数(二)

深度学习参数初始化(二)Kaiming初始化 含代码

01 knapsack interview questions series (I)

Genesis and bluerun ventures have in-depth exchanges

Machine learning (I) Wu enda

微机原理与技术接口 实验四 子程序及中断实验

ros(26):ros::Time::now(),ros::Duration,toSec(),toNSec(); Calculate program execution time

超声波传感器(CHx01) 学习笔记 Ⅲ - I2C读写操作
随机推荐
OpenCV 教程 03: 如何跟踪视频中的某一对象
第二天实验
超声波传感器(CH101&ch201) - Ⅱ
C # from introduction to mastery Part II: C # basic grammar
String correlation function (II)
Linux下MySQL的安装与使用
编辑技巧篇
【Flutter】dart:一些不容忽视的特性
李宏毅《机器学习》|1. Introduction of this course(机器学习介绍)
说说 Redis 缓存穿透场景与相应的解决方法
3. Golang string type
数据库每日一题---第25天:银行账户概要 II
LeetCode_前缀和_中等_523.连续的子数组和
Acwing4405. 统计子矩阵
C#从入门到精通之第二篇: C# 基础语法
Microcomputer principle and technical interface experiment five basic IO operation temperature control experiment
阿荷投资的思考
C language drawing example - flower pattern
Hcip fourth day notes
Ah Qu's thinking