将数组拼接成两半,无论大小? [英] Splice an array in half, no matter the size?
本文介绍了将数组拼接成两半,无论大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,我想分成两半.所以我可以将前半部分放在右侧,将后半部分放在左侧.
I have an array I would like to split in half. So I can position the first half on the right side and the second half on the left side.
我之前用过拼接功能:
var leftSide = arrayName.splice(0,2);
但不知道如何将其拼接成一半大小,因为数组的大小会动态变化.
But not sure how to splice it in half no matter the size, because the array will change in size dynamically.
推荐答案
var half_length = Math.ceil(arrayName.length / 2);
var leftSide = arrayName.splice(0,half_length);
在轨道评论中编辑@Lightness Races 后的代码
edited the code following @Lightness Races in Orbit comment
这篇关于将数组拼接成两半,无论大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文