Split array into two arrays

前端 未结 9 926
甜味超标
甜味超标 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:08

    Yet another suggestion:

    var segments = arr.join( '' ).split( point ).map(function( part ) {
        return part.split( '' );
    });
    

    now segments contains an array of arrays:

    [["a", "b"], ["d", "e", "f"]]
    

    and can get accessed like

    segments[ 0 ]; // ["a", "b"]
    segments[ 1 ]; // ["d", "e", "f"]
    

提交回复
热议问题