不管大小如何,将阵列拼接成两半? [英] 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.
我以前使用过拼接功能:
I have used the splice function before:
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的Orbit注释中编辑了代码
edited the code following @Lightness Races in Orbit comment
这篇关于不管大小如何,将阵列拼接成两半?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文