js Array.from & Array.of All In One

数组生成器

Array.from

The Array.from() static method creates a new, shallow-copied Array instance from an array-like or iterable object.

Array.from()静态方法从类似数组或可迭代的对象中创建一个新的,浅复制的Array实例。

Array.from(arrayLike [, mapFn [, thisArg]])


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; const stringArr = Array.from(`xgqfrms`); log(`stringArr =`, stringArr); log(`\n`); const iteratorArr = Array.from([1, 2, 3], x => x ** x); log(`iteratorArr =`, iteratorArr); /* stringArr = [
'x', 'g', 'q',
'f', 'r', 'm',
's'
] iteratorArr = [ 1, 4, 27 ] */

const log = console.log; log(`\n`); // Array.from(arrayLike [, mapFn [, thisArg]]) const arrayLike = [1, 2, 3] const mapFn = (item) => {
log(`item = `, item);
return 2 ** item;
} const thisArg = this; const test = Array.from(arrayLike, mapFn, thisArg); log(`test =`, test); /* item = 1
item = 2
item = 3
test = [ 2, 4, 8 ] */

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

Array.of

The Array.of() method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

Array.of()方法从可变数量的参数创建新的Array实例,而不考虑参数的数量或类型。

Array.of(element0[, element1[, ...[, elementN]]])


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; const arrOf1 = Array.of(7);
const arrOf2 = Array.of(1, 2, 3); log(`arrOf1 = `, arrOf1)
log(`arrOf2 = `, arrOf2) const arr1 = Array(7);
// array of 7 empty slots
const arr2 = Array(1, 2, 3); log(`\n`)
log(`arr1 = `, arr1)
log(`arr2 = `, arr2) /* arrOf1 = [ 7 ]
arrOf2 = [ 1, 2, 3 ] arr1 = [ <7 empty items> ]
arr2 = [ 1, 2, 3 ] */

polyfill


if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
// Or
let vals = [];
for(let prop in arguments){
vals.push(arguments[prop]);
}
return vals;
}
}

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

js no for creating a 100 length array

js no for 100 array

padStart


// string arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1).map((item, i) => `` + item); // (100) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"] // number arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1); // (100) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

Uint8Array & Typed Arrays

const arr = new Uint8Array(100).map((item, i) => i);

// Uint8Array(100) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

refs

js no for 100 array

https://www.cnblogs.com/xgqfrms/p/8982974.html

https://stackoverflow.com/questions/3746725/how-to-create-an-array-containing-1-n/64754401#64754401



xgqfrms 2012-2020

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


js Array.from & Array.of All In One的更多相关文章

  1. JS中数组Array的用法示例介绍 (转)

    new Array() new Array(len) new Array([item0,[item1,[item2,...]]] 使用数组对象的方法: var objArray=new Array() ...

  2. 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?

    在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...

  3. js中的Array

    js中的Array 啥是ArrayLike对象 类似,下面这种对象的就是ArrayLike var arraylike = { 0: "a", 1: "b", ...

  4. JS arguments转array

    JS arguments转array? Array.prototype.slice.call(arguments)

  5. JS数组(Array)操作汇总

    1.去掉重复的数组元素.2.获取一个数组中的重复项.3.求一个字符串的字节长度,一个英文字符占用一个字节,一个中文字符占用两个字节.4.判断一个字符串中出现次数最多的字符,统计这个次数.5.数组排序. ...

  6. [JS] ECMAScript 6 - Array : compare with c#

    扩展运算符(spread) 先复习下 rest 参数. (1) argument模式,但不够好. // https://blog.csdn.net/weixin_39723544/article/de ...

  7. js中关于array的常用方法

    最近总结了一些关于array中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. 第 ...

  8. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  9. Array.fill & array padding

    Array.fill & array padding arr.fill(value[, start[, end]]) https://developer.mozilla.org/en-US/d ...

随机推荐

  1. 提示框,对话框,路由跳转页面,跑马灯,幻灯片及list组件的应用

    目录: 主页面的js业务逻辑层 主页面视图层 主页面css属性设置 跳转页面一的js业务逻辑层 跳转页面一的视图层 跳转页面二的视图层 跳转页面三的js业务逻辑层 跳转页面三的视图层 跳转页面三的cs ...

  2. 3、剑指offer-数组——数组中重复的数字

    *题目描述* **在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输 ...

  3. odoo-nginx 配置

    p.p1 { margin: 0; font: 12px "Andale Mono"; color: rgba(40, 254, 20, 1); background-color: ...

  4. P4826

    总的来说, 这道题只考查了单纯的建图和最大生成树 但这却是蓝题(问号 题意 题意的理解比较麻烦 简单说就是 n 支队伍比赛,i 号队伍和 j 号队伍比赛可获得 i ^ j 的分数,然后其中一支队伍会输 ...

  5. CODEVS 2542单词__fail树

    2542 单词 2013年省队选拔赛天津市队选拔赛  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 小张最近在忙毕 ...

  6. 你可能不知道的 transition 技巧与细节

    CSS 中,transition 属性用于指定为一个或多个 CSS 属性添加过渡效果. 最为常见的用法,也就是给元素添加一个 transition,让其某个属性从状态 A 变化到状态 B 时,不再是非 ...

  7. Grafana+Prometheus通过node_exporter监控Linux服务器信息

    Grafana+Prometheus通过node_exporter监控Linux服务器信息 一.Grafana+Prometheus通过node_exporter监控Linux服务器信息 1.1nod ...

  8. Spark JDBC方式连接MySQL数据库

    Spark JDBC方式连接MySQL数据库 一.JDBC connection properties(属性名称和含义) 二.spark jdbc read MySQL 三.jdbc(url: Str ...

  9. Be accepted for inclusion in the IEEE INFOCOM 2018 technical program

    中了一篇INFOCOM,虽然不是一作但也是入学之后一直做的一份工作,算是没白下功夫吧.超声波定位这类工作,老实说,想应用到实际产品中,还是有一段路要走的. 老实说我也一直犹豫毕设的这套东西搞清楚了要不 ...

  10. Cisco动态路由(rip)

    接Cisco静态路由,讨论一下Cisco动态路由. 实验环境布置 命令布置动态路由 Router0: Router>enable Router#configure terminal Router ...