Split array into two arrays

前端 未结 9 947
甜味超标
甜味超标 2020-12-24 10:24
var arr = [\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'];
var point = \'c\';

How can I split the \"arr\" into two arrays based on the \"point\" variabl

9条回答
  •  醉话见心
    2020-12-24 11:26

    var arr2 = ['a', 'b', 'c', 'd', 'e', 'f'];
    arr = arr2.splice(0, arr2.indexOf('c'));
    

    To remove 'c' from arr2:

    arr2.splice(0,1);
    

    arr contains the first two elements and arr2 contains the last three.

    This makes some assumptions (like arr2 will always contain the 'point' at first assignment), so add some correctness checking for border cases as necessary.

提交回复
热议问题