数组
一维数组拆分为多维数组:
方式一:采用forEach遍历数组,向下取值push进去
方式二:用for循环, 选取指定范围内值,push进去
typeof和instanceof
对于值类型,你可以通过typeof判断,string/number/boolean都很清楚,但是typeof在判断到引用类型的时候,返回值只有object/function,你不知道它到底是一个object对象,还是数组,还是new Number等等。
var items = []; var object={}; function reflect(value) { return value } console.log(typeof items); //object console.log(typeof object); //object console.log(typeof reflect); //function //以上我们应用typeof 运算符变量返回了对象。这不是很有用,因为我们有时想知道对象表示的类型。 // instanceof 运算符接受一个对象和一个构造函数参数。 // 当值是构造函数指定的类型的实例时,instanceof returns true; 否则,它返回false console.log(items instanceof Array); // true console.log(object instanceof Object); // true console.log(reflect instanceof Function);// true
charAt() 方法可返回指定位置的字符。
字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该方法将返回一个空字符串。
var word = "hello world" var wordChar = word.charAt(0); console.log(wordChar); //h
数组总结(一)
给数组追加、删除项实现方法:
- 索引方法
- length方法
- push()、pop()方法
- splice()方法
基于当前数组创建新数组:
- concat()
- slice()
js数组遍历一次,删除部分元素
for (var i = groupBuyList.length - 1; i >= 0; i--) { //倒叙 if (groupBuyList[i].remain_string == 0){ groupBuyList.splice(i,1) } }
参考: