在开发项目中,经常会遇到对数组的操作,比如对数组的数据进行删减或增加,同时也会对每个数组里的数据进行删减,下面就举个例子说明一下。
直接给一段代码
上面是根据onlineId获取表单列信息,
获取的信息如下:
但这里的"columns":不能直接给给this.columns,否则会报错,所以需要进行处理
首先要删除不需要的列
变成下面的列
同时根据要求,在前面要增加一组信息indexColumn,内容如下:
最后合成如下数组:
另外附上其它一些数组的相关方法如下
1、将数组合并成字符串(返回字符串)[ Array.join() ]
2、返回逆序数组(倒叙排列数组)[ Array..reverse() ]
3、Array.concat(),创建并返回一个新数组
4、Array.slice()方法,返回指定数组的片段或者子数组。不会改变原数组
5、Array.splice()方法,用来删除或插入元素,会修改原数组!
可以用来解决在数组起始部位加入数据
6、push()方法与pop()方法
push()方法在数组的尾部添加一个或者多个元素,并返回数组的新长度。注意的是,改变的是原数组的值,返回的是新数组的长度。
pop()方法删除数组的最后一个元素,并返回它的删除值。也是改变原数组,返回的是删除的值。
7、unshift()方法与shift()方法
unshift()方法类似于push()不同的是,他是在数组头部添加,其他都一样
shift()方法则类比pop()方法。
8、forEach()方法,从头至尾遍历数组,为每个元素调用指定的函数。
9、map()方法
和forEach()类似,调用数组的每个元素传递给指定函数,并返回一个数组,所以它和forEach()的区别在于,有一个返回值。不修改原数组,返回的数组长度和原数组相同
10、filter()方法,返回的是调用数组的一个子集
filter()会跳过稀疏数组中缺少的元素,他的返回数组总是稠密的。所以可以用一下方法来压缩稀疏数组的空缺。