加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

JS语法:几种循环 生成100长度数组

发布时间:2020-12-15 00:13:40 所属栏目:C语言 来源:网络整理
导读:javascript中的对象为什么会按照键来自动排序? object数据存储时没有顺序的。 你说的输出是for之类的吧, 这个应该是跟浏览器有关的。 打断forEach循环 按length来,每次跳过空 (内部某种方式变dataArr.length,变小有效,变大无效) 意味着forEach等,可以被

javascript中的对象为什么会按照键来自动排序?

object数据存储时没有顺序的。
你说的输出是for之类的吧, 这个应该是跟浏览器有关的。

打断forEach循环

按length来,每次跳过空(内部某种方式变dataArr.length,变小有效,变大无效)
意味着forEach等,可以被打断,不能被增加。

> var a = [1,2,3]
undefined
> a.length = 1
1
> a.forEach(() => console.log('test'))
test
//改变了次数
> var a= [1,3]
undefined
> a.forEach(function(item) {
... a.shift();
... console.log('test');
... })
test
test
//不改变次数
> a.forEach(function(item) {
... a.push('1');
... console.log(item);
... })
1
2
3

也就是说

实际每次也是重新读取a.length属性,但只允许比a.length初始值小。(大的话,还是初始值)

var a = [1,3,4];
a.forEach(function(item,index) {
    if (item === 1) a.length = index;
    console.log('test');
});
var a = [1,index) {
    if (item === 1) a.length = 10;
    a.push(5);
    console.log('test');
});
console.log(a,a.length);
a.forEach(function(item,index) {
    console.log(index);
})
/*
[ 1,4,5,5 ] 14
0
1
2
3
10
11
12
13
*/

对象与数组 循环

for in 专门用于对象循环。(尽管IE存在兼容性问题)

for in 在数组循环上的历史遗留问题,
使得forEach(for of)与数组门当户对.

> a.forEach(function(item) {console.log(item)})
A
B
C

for (let i in a) { console.log(a[i])}
A
B
C
Hello

for (let i of a) { console.log(i)}
A
B
C

循环次数

for(let i=0; i < len; i+=)都好理解,主要是forEach for(let i in data)容易被忽略,用错。

function cleanRootAllId(el) {
let nodes = [el];
nodes.forEach(function(node) {
node.setAttribute('id','');
nodes = nodes.concat(Array.prototype.slice.call(node.children));
});
console.log(nodes);
}

forEach/for(let i in data)在 callbock(回调函数)内部更改nodes的值,不会改变循环次数

forEach即for(let i in data)、 for(let i in data)的循环次数相当于 for (let i=0; i < data.length; i++)

> arr
[ 2,'' ]
arr.reduce(function(prev,item) {console.log(item); prev[1] = 'test'; prev[3] = '1'; return prev},arr)
2
test
[ 2,'test' ]
> for(let i in a) {
... a.push(1);
... console.log(a);
... }
[ 1,1 ]
[ 1,1,1 ]

data.length直接绑定,data.length被取出,且不再更改。

function cleanRootAllId(el) {
let nodes = [el];
let len  = nodes.length;
for (let i=0; i < len; i++) {
nodes[i].setAttribute('id','');
nodes = nodes.concat(Array.prototype.slice.call(nodes[i].children));
len = nodes.length;
};
}

len = nodes.length;在内部更改len值,可实现变更遍历次数。

reduce/forEach/map等循环次数

function getIncrArr(count) {
const template = [null];
template.length = count;
template[100] = undefined;
return template.reduce(function(stock,item,index) {
return stock.concat([index]);
},[]);
}

const res = getIncrArr(100);
console.log(res); //输出 [0, 100]

以上可以看出,这几个接口只循环填过值的

for in循环次数

function getIncrArr(count) {
    const template = [null];
    template.length = count;
    template[100] = undefined;
    for (let index in template) {
        console.log(index);
    }
}

const res = getIncrArr(100);
console.log(res);
//0
//100
//undefined

以上可以看出,for in只循环填过值的

while

while(let input = read_line()) {    //error,while括号内必须是表达式

let input;
while(input = read_line()) { //Right

方式一

var arr = new Array(100);
for(var i = 0;i < 100;i++){
arr[i]='abc';
}

NO!,一点意思也没有,而且,看起来好像很耗时的样子。。(同意!)

方式二

前面说了,reduce/for in等不识别未填值的项([,])。但join识别啊!曲线解决方式来了~~

> var arr = [];

arr.length = 50
50
arr.join()
','

> arr
[,]
arr.join().split()
[ ',' ]
arr.join().split('')
[ ',',' ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读