Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  61] [ 5]  / answers: 1 / hits: 155792  / 12 Years ago, thu, march 29, 2012, 12:00:00
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';


How can I split the arr into two arrays based on the point variable, like:



['a', 'b']


and



['d', 'e', 'f']

More From » jquery

 Answers
27
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.


[#86524] Wednesday, March 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adilene

Total Points: 395
Total Questions: 88
Total Answers: 109

Location: Indonesia
Member since Tue, Aug 3, 2021
3 Years ago
;