js Array All In One

array 方法,改变原数组(长度),不改变原数组(长度)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

  1. static 方法: Array.isArray / Array.of / Array.from

  2. property: length

  3. 改变原来 array (原数组长度): unshift / push / shift / pop / splice / copyWithin / fill

  4. 改变原来 array (原数组): sort / reverse

  5. 不改变原来 array长度:

访问器方法: slice / filter / join / concat / includes / indexOf / lastIndexOf / toString / toSource / toLocaleString & flat / flatMap

迭代器方法: entries / keys / values / every /some / find / findIndex / map / reduce / reduceRight

demos

reverse

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

const log = console.log;

const arr = ['one', 'two', 'three'];
log('\narr =', arr);
// ["one", "two", "three"] const reversed = arr.reverse();
log('\nreversed =', reversed);
// ["three", "two", "one"] // ️ Careful: reverse is destructive -- it changes the original array.
// ️ 注意:reverse是破坏性的-它会更改原始数组
log('\nnew arr =', arr);
// ["three", "two", "one"]

splice

splice 改变原数组的长度

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice


const log = console.log; const arr = [1, 2, 3, 4, 5, 6, 7]; let mid = arr.splice(Math.floor(arr.length / 2), 1);
let value = mid[0]; log(`mid arr =`, mid)
log(`mid value =`, value) log(`new arr =`, arr) /* mid arr = [4]
mid value = 4 new arr = (6) [1, 2, 3, 5, 6, 7] */

slice

slice 不改变原数组的长度

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice


const log = console.log; const arr = [1, 2, 3, 4, 5, 6, 7]; let mid = arr.slice(Math.floor(arr.length / 2), Math.floor(arr.length / 2) + 1);
let value = mid[0]; log(`mid arr =`, mid)
log(`mid value =`, value) log(`new arr =`, arr) /* mid arr = [4]
mid value = 4 new arr = (7) [1, 2, 3, 4, 5, 6, 7] */

of

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of


from

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from


reduce

累加器 acc

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce


const log = console.log; const arr = [1, 2, 3, 4, 5, 6, 7]; const sum = arr.reduce((acc, item) => acc+= item, 0); log(`sum =`, sum); // sum = 28

flat & flatMap

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

const arr1 = [1, 2, [3, 4]];

arr1.flat();
// (4) [1, 2, 3, 4] arr1;
// (3) [1, 2, Array(2)]
const arr = [1, 2, 3, 4];

arr.flatMap(x => [x, x * 2]);
// (8) [1, 2, 2, 4, 3, 6, 4, 8] arr;
// (4) [1, 2, 3, 4]
const arr = [1, 2, 3, 4];

arr.flatMap(x => [x, x * 2]);
// is equivalent to arr.reduce((acc, x) => acc.concat([x, x * 2]), []);
// [1, 2, 2, 4, 3, 6, 4, 8]

for loop

for / forEach / for in / for of

for in === Object

for of === array / NodeList / Set / Map ...

for in

Object.hasOwnProperty 过滤从 proto / prototype 上面继承的可枚举属性

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

for ... in语句迭代对象的所有可枚举属性(包括继承的可枚举属性),这些可枚举属性由字符串键入(忽略由Symbol键入的属性)。

for...in 支持 Array

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description for...in & Object.hasOwnProperty
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
* @solutions
*
* @best_solutions
*
*/ const log = console.log; const obj = {
a: 1,
b: 2,
}; // obj.prototype.c = 3;
log(`obj.prototype =`, obj.prototype)
// obj.prototype = undefined obj.__proto__.c = 3;
log(`obj.__proto__ =`, obj.__proto__)
// obj.__proto__ = {} log(`\n`) const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) {
log(`keys[${i}] =`, obj[keys[i]]);
} log(`\n`) for (const key in obj) {
if (obj.hasOwnProperty(key)) {
log(`key =`, obj[key]);
} else {
log(`__proto__ key =`, obj[key]);
}
} /* obj.prototype = undefined
obj.__proto__ = { c: 3 } keys[0] = 1
keys[1] = 2 key = 1
key = 2 */
const arr = [1, 2, 3, 4, 5, 6, 7];

log(`\n`)

for (const key in arr) {
// for...in & array
log(`index =`, key, arr[key] );
// 包含Object 上,包括原型链上继承的所有可枚举的 string 属性
log(`typeof(item) =`, typeof(item));
// typeof(item) = string
} /* index = 0 1
index = 1 2
index = 2 3
index = 3 4
index = 4 5
index = 5 6
index = 6 7
index = c 3 */ log(`\n`) for (const item in arr) {
// log(`typeof(item) =`, typeof(item));
// typeof(item) = string
if(item === "3") {
// for...in & break
log(`for...in break =`, item);
break;
} else {
log(`item =`, item);
}
} /* item = 0
item = 1
item = 2
for...in break = 3 */

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

for of

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.

for...of 不支持 Object


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
* @solutions
*
* @best_solutions
*
*/ const log = console.log; const obj = {
a: 1,
b: 2,
}; // TypeError: obj is not iterable
// for (const key of obj) {
// // for...of & object
// log(`key =`, obj[key]);
// } const arr = [1, 2, 3, 4, 5, 6, 7]; log(`\n`) for (const item of arr) {
// for...of & array
log(`item =`, item);
} log(`\n`) for (const item of arr) {
if(item === 3) {
// for...of & break
log(`for...of break =`, item);
break;
} else {
log(`item =`, item);
}
} /* item = 1
item = 2
item = 3
item = 4
item = 5
item = 6
item = 7 item = 1
item = 2
for...of break = 3 */

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

forEach


refs

MDN Array

https://img2020.cnblogs.com/blog/740516/202004/740516-20200428000822349-2064140300.png



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js Array All In One的更多相关文章

  1. js Array数组的使用

    js Array数组的使用   Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...

  2. 从Chrome源码看JS Array的实现

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...

  3. js Array 方法总结

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. js & array to string

    js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...

  5. From Ruby array to JS array in Rails- 'quote'?

    From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>

  6. 解决js array的key不为数字时获取长度的问题

    最近写js时碰到了当数组key不为数字时,获取数组的长度为0 的情况. 1.问题场景 var arr = new Array(); arr[‘s1‘] = 1001; console.log(arr. ...

  7. JS Array.reverse 将数组元素颠倒顺序

    <pre><script type="text/javascript"> //JS Array.reverse 将数组元素颠倒顺序//在JavaScript ...

  8. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

  9. js array & unshift === push head

    js array & unshift === push head const arr = [1, 2, 3]; console.log(arr.unshift(4, 5)); // 5 con ...

  10. js Array.from & Array.of All In One

    js Array.from & Array.of All In One 数组生成器 Array.from The Array.from() static method creates a ne ...

随机推荐

  1. MySQL设计之Schema与数据类型优化

    一.数据类型优化 1.更小通常更好 应该尽量使用可以正确存储数据的最小数据类型,更小的数据类型通常更快,因为它们占用更少的磁盘.内存和CPU缓存,并且处理时需要的CPU周期更少,但是要确保没有低估需要 ...

  2. Before you launch a goroutine, know when it will stop The Zen of Go

    The Zen of Go https://the-zen-of-go.netlify.app/ Ten engineering values for writing simple, readable ...

  3. git 分支合并时如何忽略某个文件

    [转]git 分支合并时如何忽略某个文件 - 神奇的旋风 - 博客园 https://www.cnblogs.com/xuan52rock/p/13268872.html Git - git-merg ...

  4. 初识 Nginx服务配置

    Nginx 是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名.很多高知名度的网站 ...

  5. lodash的debounce函数的使用

    最新,在react新项目的开发中使用到了lodash类库的debounce方法,就随手梳理了一下此方法的方便之处 未使用debounce之前 如果不考虑使用debounce,那么在用户连续点击的情况之 ...

  6. 客户端,Scala:Spark查询Phoenix

    客户端,Scala:Spark查询Phoenix 1.pom.xml 2.配置文件 2.1config.properties 2.2MyConfig 3.entity实体(与phoenix中的tabl ...

  7. Java实现发送HTTP的POST请求,返回数据以及请求状态

    /** * @param url:请求url * @param content: 请求体(参数) * @return errorStr:错误信息;status:状态码,response:返回数据 */ ...

  8. NoSQL:一个帝国的崛起

    01关系数据库帝国 现在是公元2009年,关系帝国已经统治了我们30多年,实在是太久了. 1970年,科德提出关系模型,1974年张伯伦和博伊斯制造出了SQL ,帝国迅速建立起了统治. 从北美到欧洲, ...

  9. [The Preliminary Contest for ICPC Asia Shanghai 2019] B-Light bulbs(差分+思维)

    前言 最近有很多算不上事的事,搞得有点心烦,补题难免就很水,没怎么搞,自我检讨一番~~ 说实话网络赛题目的质量还是挺高的,题目都设计的挺好的,很值得学习.这场比赛那会只有我们大二的在做,其他人去参加$ ...

  10. Educational Codeforces Round 102 (Rated for Div. 2) D. Program (思维,前缀和)

    题意:给你一个只含\(+\)和\(-\)的字符串,给你一个数\(x\),\(x\)初始为\(0\),随着字符串的遍历会加一减一,现在有\(m\)个询问,每个询问给出一个区间\([l,r]\)表示将这个 ...