当前位置:网站首页>JS some small problems about adding and accessing values to arrays
JS some small problems about adding and accessing values to arrays
2022-06-25 18:36:00 【ookaoooo】
Some small problems about adding and accessing values to arrays
Today, let's take a look at some small problems about arrays , It may help you a little , Of course, if I say something wrong, I'm also welcome to give advice , After all, I am also a rookie .
arr["bbb"]="nor 1";
arr[-2]="nor 2";
console.log(arr); >> [1, 2, 3, bbb: "nor 1", -2: "nor 2"]
console.log(arr.bbb) >> "nor 1"</pre>If we want to add a value to the array , With [] In the form of , If you write a negative number or a string, it adds... To the end of the array , And it is added in the form of key value pairs , So the next time you access this value, you can access it in the form of points , But if it is a number, it must pass [] visit .
arr["bbb"]="nor 1";
console.log(arr); [1, 2, 3, bbb: "nor 1"]
console.log(arr[3]) undefined</pre>If you add a value to an array through a string or a negative number , The next time you access it, you must also access it in the form of key value pairs
arr["bbb"]="nor 1";
arr[-2]=222;
arr.push(4);
console.log(arr); >> [1, 2, 3, 4, bbb: "nor 1"]
console.log(arr.length); >> 4</pre>It is worth noting that values added by strings or negative numbers , That array will not add its length , And those added in this way will always be at the end of the array , Because we use push Method to add a number 4 We found that it didn't add "is" to the end , Everybody knows push Method to add a value to the end of the array . Maybe we can draw a conclusion that is numbers and their arrangement , Key value pairs and key value pairs are arranged .
Update array small problem .
num.push(4,3,5); >> The return value is the last number added, that is, the number 5
num.reverse(); >> Array in reverse order , Not by size , It's the other way around
console.log(num) >>[5, 3, 4]</pre>num[5,"a",0]="111"; >>["111"] If the end says 0 Or array length plus 1 Well, as usual .
console.log(num);</pre>num[5,"a",6]="111";
console.log(num); >>[6: "111"]</pre>The back covers the front , The index written to the last bit cannot be greater than the length of the array +1, Otherwise, whether you write numbers or not, they are added through key value pairs , If it's negative, it's the same .
a[10] = 10;
console.log(a); >>[10: 10]
console.log(a.length); >>11
console.log(a[0]); >>undefined
console.log(a[10]); >>10</pre>If the length of the array is 0 Or not as long as the index you want to add , that js All previous values will be set to undefined, And it is added in the form of key value pairs .
边栏推荐
- 05 virtual machine stack
- Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]
- 【深入理解TcaplusDB技术】单据受理之建表审批
- 网络安全检测与防范 测试题(五)
- 网络安全检测与防范 测试题(四)
- . Net worker service adding a serial log record
- Differences and relations between sequential table and array (easy to confuse), linear table and linked list
- Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]
- Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
- Analysis on China's aluminum foil output, trade and enterprise leading operation in 2021: dongyangguang aluminum foil output is stable [figure]
猜你喜欢

03 runtime data area overview and threads

Detailed explanation of oauth2 - Introduction (I)

【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩

How can the self-supporting number evaluation be safer for cross-border e-commerce platforms such as Amazon, eBay, shopee, lazada, express, Wal Mart and Alibaba international?
![2021 development status of China's cloud game industry and analysis of major service providers: Although cloud games are still in their infancy, the market prospect is huge [figure]](/img/82/84072a7b125f81363fb26ac56e9d27.jpg)
2021 development status of China's cloud game industry and analysis of major service providers: Although cloud games are still in their infancy, the market prospect is huge [figure]

使用宝塔来进行MQTT服务器搭建
![Analysis on development scale and development trend of China's night economy industry in 2021 [figure]](/img/57/d6821fca9a608bf33e78287cf26bae.jpg)
Analysis on development scale and development trend of China's night economy industry in 2021 [figure]

华为发布两大昇腾计划 推动AI人才发展和科研创新

Redis 5.0 data structure double end linked list source code analysis

Ruffian Heng embedded semimonthly issue 57
随机推荐
RMAN backup database_ Manage backup window
Command records of common data types for redis cli operations
JVM|运行时数据区(堆空间)
Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
Use pagoda to set up mqtt server
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
《痞子衡嵌入式半月刊》 第 57 期
2021 development status of China's cloud game industry and analysis of major service providers: Although cloud games are still in their infancy, the market prospect is huge [figure]
05 virtual machine stack
一晚上做了一个xpath终结者:xpath-helper-plus
Detailed explanation of route add command
03 runtime data area overview and threads
How can the self-supporting number evaluation be safer for cross-border e-commerce platforms such as Amazon, eBay, shopee, lazada, express, Wal Mart and Alibaba international?
网络安全检测与防范 练习题(三)
正则表达式总结
GNU nano
Dell r530 built in hot spare status change description
RMAN backup database_ Use RMAN for split mirror backup
[elt.zip] openharmony paper Club - memory compression for data intensive applications
GNU nano