javascript:array数组

来源:互联网 发布:mac系统ai2018破解码 编辑:程序博客网 时间:2024/05/14 09:31

简单记录下arra的使用方法

<script>/* 1.明白数组与object的区别(有序与无序)数组指的是有序的集合,而对象值得是无序的集合 2.明白为什么添加了object之后会长度不变 3.length的可读以及可写性质 4.pop作用 :请注意,pop方法是会减少length的长度的 5.push方法添加在尾部,会增加他的长度的 6.shift方法可以移除第一项,当然这个也是会减少长度 7.unshift方法会在数组的第一项增加一个新的项,而且会增加他的长度 8.object添加项的方法 */console.log(1 === +1);//length表示的是数组所占内存空间的数目,而不仅仅是数组中元素的个数//length不仅仅是可读的,他还是可写的,可以通过他来控制长度。。。//也可以通过length来增加他的长度//pop最后一个项var c = new Array();c.push(1);c.push(2);c.push(3);c.push(4);//对象var a = [1, 2, 3, 4];//数组var b = { 0: 1, 1: 1, 2: 2, 3: 3 };console.log(a);console.log(b);console.log(c);console.log(a[3]);console.log(b[3]);a["foo"] = "hello world"console.log(a);console.log(a.length);b["footer1"] = "footer1"b.footer2 = "footer2"console.log(b);console.log(Object.keys(b).length);c[c.length] = "hello,world";c[5] = "hello world"//c["footer"] = "footer";console.log(c);console.log(c.length);c.length = 2;console.log(c);console.log(c.pop());console.log(c.shift());console.log(c.unshift("bew_1_by_unshift"));console.log(c);console.log(c.length);c.footer2="footer2";console.log(c);console.log(c.footer2);</script>

/* * 1.降序(reverse是将顺序直接颠倒,而不是根据内容。。。) * 2.升序(sort)是根据内容升序的, * 3.splice方法删除从哪一个开始的,攻击多少个的长度 * 4.第三个参数可以设置为替换,但是只是一个替换而已 */var myarray = Array(9, 1, 8, 2, 3, 7, 4, 6, 5, 0);console.log(myarray);console.log(myarray.length);console.log(myarray.sort());myarray.reverse();myarray.splice(2,3);console.log(myarray);myarray.splice(2,5,"hello");console.log(myarray);