empty array & Array.from
empty array bug
const duplicationArray = (arr = [], times = 2, debug = false) => {
let result = [];
let temps = new Array(times);
console.log(`temps =`, temps);
temps.forEach(
(item, i) => {
// undefined bug
console.log(`item =`, item);
let temp = arr;
result.concat(temp);
console.log(`result =`, result);
}
);
// for (let i = 0; i < times; i++) {
// let temp = arr;
// result.concat(temp);
// }
if (debug) {
console.log(`result =`, result.length);
}
return result;
};
solution
Array.from()
bug
let temps = new Array(3);
// (3) [empty × 3]
temps.forEach(
(item, i) => {
// empty & undefined bug
console.log(`item =`, item, i);
}
);
OK
let temps = Array.from(new Array(3));
// (3) [undefined, undefined, undefined]
temps.forEach(
(item, i) => {
// undefined ok
console.log(`item =`, item, i);
// item = undefined 0
}
);
empty array & Array.from的更多相关文章
- c++ - Create empty json array with jsoncpp - Stack Overflow
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 multiprocessing.pool c++ - Create empty json array wit ...
- array_flip() array_merge() array+array的使用总结
array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...
- PHP中array_merge函数与array+array的区别
在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,下面为大家介绍两者具体的使用区别. 区别如下: 当下标为数值时,array_me ...
- hdu 6197 array array array
array array array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- PHP中array_merge和array+array的区别
在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,区别如下: 当下标为数值时,array_merge()不会覆盖掉原来的值,但ar ...
- php 通过array_merge()和array+array合并数组的区别和效率比较
众所周知合并两个数组可以使用array_merge(),这是php提供的一个函数.另外还可以通过 array 的方式来合并数组,这两种直接有什么区别,哪一个的效率更高呢? array_merge() ...
- PHP array_flip() array_merge() array+array的使用总结
array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...
- php中数组直接用加号相加array+array
php中数组功能非常强大,甚至也可以直接通过+相加来合并数组. A数组 $a = ['a', 'b']; B数组 $b = ['c', 'd', 'e']; A+B结果 Array ( [0] =&g ...
随机推荐
- Java properties配置文件
Java中的配置文件常为properties文件,格式为文本文件,文件内容的格式是“键=值”格式.注释信息使用“#”来注释. Properties类的常用方法 String getProperty(S ...
- Ruby和Swift的Range
意义 Swift Ruby [1, 2, 3, 4, 5] 1...5 1..5 [1, 2, 3, 4] 1..<5 1...5 ...
- bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】
高端贪心,好久没写splay调了好久-- 以下v为价格,w为鲜嫩度 把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当 ...
- c++ isdigit函数
函数名:isdigit 函数所需头文件:#include<cstdio> 函数格式:isdigit(字符) 函数作用:判断括号内是否为1~9的数字. 例:isdigit(4) 就是true ...
- fread/fwrite实现复制功能
1. fread/fwrite实现复制功能 #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 4096 //执行 ...
- FJOI2019退役记
day1 不意外地一点都不紧张,早就感觉没有机会了吧 进场非常从容地读完了三道题,开始写t1暴力,接着就开始自闭,不知道该开t2还是t3,最后先开了t3,想了想这不是选两条不相交的链吗,这个暴力不是林 ...
- Elasticsearch--建议器
目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...
- V形
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- 类支付宝密码输入框NumberEditText(简单粗暴的定制方式)
因为项目需要,设计了一个下图样的验证码输入框(ps:个人认为还不如直接一个EditText,用户友好度可能更好,何况这页面99.9%的用户不会使用,但是没办法,别人才是专业的设计师). 其实界面很简单 ...
- Xamarin绑定ios静态库
以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...