Split array into two arrays

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

    Try this one:

    var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
    var point = 'c';
    
    var idx = arr.indexOf(point);
    
    arr.slice(0, idx) // ["a", "b"]
    arr.slice(idx + 1) // ["d", "e", "f"]
    

提交回复
热议问题