当前位置:网站首页>JS basic knowledge collation - array
JS basic knowledge collation - array
2022-07-27 13:30:00 【Monkey seven】
Recently, I feel that I don't have a solid grasp of basic knowledge , If you encounter an interception and replacement, you have to check , Forget it after a long time , Find out the notes you used to take after watching the video tutorial , You can also review while sorting .
One 、 Array
use [ ] A group of data packed together is called an array
He has an index length It has its own attribute ( So called ergodic loop , Is the use for loop , Realize the traversal of the array )
Array public methods (14 individual )
Any of our arrays can call , There is Array Prototype prototype Common methods on , Because every array is Array An instance of the array class ;
1、 Array of “ Additions and deletions ” The original array changes
1)push The return value is the length of the array after adding a new item
var ary=[1,2,3,4,5];
var res=ary.push(4,5,6,8);
console.log(ary,res);

Tips
ary[ary.length]="10";
console.log(ary);

adopt ary.length The index implementation adds a new entry to the array
2)unshifit The return value is the length of the array after adding a new item
Add items to the beginning of the array
var ary=[1,2,3,4,5];
var res=ary.unshift(0);
console.log(ary,res);

3)pop
Delete the last item The return value is delete item
Tips : utilize length Property to delete the last item of the array ary.length-=1; ary.length–; Both have the same effect
4)shift
Delete the beginning of the array The return value is delete item
var ary=[1,2,3,4,5];
var res=ary.shift();
console.log(ary,res);
5)splice Return a crop item to form a new array
Crop the array
grammar :ary.splice( Crop index , How many items to cut , What should be used to replace the crop item )
var ary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = ary.splice(3, 2, 0);
console.log(ary, res);

var ary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = ary.splice(3);// From the index 3 Start deleting all subsequent items
console.log(ary, res);

Delete the last item
ary.splice(ary.length-1,1); ary.splice(ary.length-1);
Add... At the end
var res=ary.splice(ary.length,0,"a");
Delete 0 term add to “a” Return an empty array Because I didn't delete anything
Add at the beginning
var res=ary.splice(0,0," Opening item ")
Replacement and addition of intermediate items
Replaceary.splice(0,1,"a");Delete replace 1 term
add toary.splice(3,2,"c","d");Delete and replace multiple add to “c” “d”
Increase is to increase the index in front of the current crop
var ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]; varres=ary.splice(1,0," I "," yes "," Two ");console.log(ary);

ary.splice(0)Clone arrayary.splice()Pass nothing on Create an empty array , Return an array instance
6)slice(n,m) Search index n Start to index m Before the item
grammar :ary.slice( Index at the beginning , The index at the end but does not contain the index entry at the current end )
stay slice Searching , The original array does not change ,slice The return value of the function is a new array composed of each found item ;
Pass only one parameter
ary.slice(0)It is equivalent to copying an arrayary.slice(0,ary.length)Clone arrayary.slice(1)Returned array It is equivalent to deleting the beginning itemary.slice(0,ary.length-1)Returned array It is equivalent to deleting the last item
( From the index 0 Start To the last Because it does not contain m The length is larger than the index 1 So the last one will stay Delete the last item )
From n term , Index correspondence (n-1), Find the first m term (m-1), Include m term , Index correspondence (m)
ary.splice() Pass nothing on and concat equally Equivalent to cloning an array
7)concat Array merge ary.concat() We can pass one , Or multiple contents , The data type of content can be arbitrary , If it's an array , It is equivalent to merging two arrays , If it's not an array , It is equivalent to adding
The original array remains the same Return a new array after merging
ary.concat("e",1,2,3)If the argument is not an array , Is to add to the end
ary.concat()Pass nothing on , It is equivalent to cloning the original array
ary.concat("e",1,2,3,{name:" Monkey seven "})Merge multiple items

Array of “ Convert to string ”
1)join The original array remains unchanged The returned result is a string separated by the specified separator , If you do not add the specified separator , The default is separated by commas 
2)toString Directly convert the array into a string 
characteristic : What is called is the method on the object prototype ( Arrays are also object data types So you can call methods on the object prototype )
Find an item in the array
1)indexOf The return value is , From the index 0 Start , Find the first position of the item in the array
2)lastindexOf The return value is , Start with the end index , Find the first position of the item in the array
Array of “ Loop traversal ” ( There are methods on the prototype for Loops are not methods that exist on prototypes )
1)forEach
Format :forEach ( Callback function )
var ary=["a","b","c"];
ary.forEach(function ( Each specific item found , The index of each item ) {
console.log(n, m);
})
2)map Traverse each item in the array , You can modify every item ; Then put each item after processing , Combine into a new array and return ; Our original array does not change
Format :
ary.map(function (item){
})
var ary=[1,2,3,4];
var res=ary.map(function (item) {
return item+"a"// One is string String splicing
});
console.log(ary, res);// ary[1, 2, 3, 4] res["1a", "2a", "3a", "4a"]
The callback function needs to use return Get the return value , Make changes to each item and return the modified results

var ary=[1,2,3,4,5];
var res=ary.map(function (item) {
return item*5; A return value is required
});
console.log(ary, res);//--->(5) [1, 2, 3, 4, 5] (5) [5, 10, 15, 20, 25]
1)sort Sort the array Sort the original array , Our return value is also an ordered array
Through formal parameters a b Compare , To achieve sorting :a b It represents every item of our array , That is, take the return value of each comparison of the array in the callback function to sort Method , Then sort the essence
var ary = [10, 22, 53, 68, 71, 83, 19];
var res=ary.sort(function (a,b) {
<--- Callback function
return a-b;
});
console.log(ary, res);

return a-b; a-bIt is to compare each item to get an array from small to largereturn b-a; b-aIt is to compare each item to get an array from large to small
ary.sort() Call directly , Without transferring the callback function , Yes, only right 10 The numbers within are sorted from small to large
According to English 26 Letters can also be sorted
> var ary=["az","ec","d5","b6","a5"];
> ary.sort();
> console.log(ary);

1)reverse Invert an inverted array
var ary=[1,2,3,4,5,6];
var res=ary.reverse();
console.log(ary, res);

User information of five people , Put the present 5 An array of individuals , Sort by age 
a b Represents each item in the array use a.age To get the age first a yes 12
边栏推荐
- leetcode——83,24;机器学习——神经网络
- 四大线程池简析
- Li Kou 1480. Dynamic sum of one-dimensional array 383. Ransom letter 412. Fizz buzz
- 高度塌陷最终解决方案(无副作用)
- 数据库HTAP能力强弱怎么看
- 计算字符串最后一个单词的长度,单词以空格隔开。
- 力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz
- SCI论文写作
- Redis summary: cache avalanche, cache breakdown, cache penetration and cache preheating, cache degradation
- 3D laser slam:aloam---ceres optimization part and code analysis
猜你喜欢

MTK6765编译环境搭建

双料第一!

滑环的分类以及用途

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(九)

Redis summary: cache avalanche, cache breakdown, cache penetration and cache preheating, cache degradation

V-on basic instruction

How to pass parameters in JNI program

常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)

纵横靶场-图片的奥秘

附加:【URLEncoder.encode(待编码字符串, “编码方式“);】(是什么?;我们向cookie中设置值的时候,为什么要使用这个去编码?)(待完善……)
随机推荐
clearfix的作用
C# FTP增、删、改、查、创建多级目录、自动重连、切换目录
libevent 之 evconnlistener_new_bind
Xshell7 can log in to MySQL virtual machine, but not mysql
《数字经济 科技向善》大咖对谈干货来啦
Qt剪切板QClipboard 复制粘贴自定义数据
v-show
四大线程池简析
Relative positioning
@Simple use of conditional
[expression calculation] double stack: general solution of expression calculation problem
51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;(只开发了【用户名+密码的,方式】;【@T…】注解控制事务;设置cookie时,是否需要使用URLEncoder去编码;)
QT excellent open source project 13: qscintilla
[node+ts] build node+typescript project
Realize the disk partition and file system mount of the newly added hard disk
元素的层级
Height collapse and BFC
Background and framework introduction and basic environment preparation of hucang integrated e-commerce project
Connotative quotations
How can the top 500 enterprises improve their R & D efficiency? Let's see what industry experts say!