Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

So I have a javascript variable that looks like this

D,CPU_Purchased_ghz,2015-03-19 00:00:00,10.00,2015-03-20 00:00:00,10.00

Is it possible to split into an array like this:

[
    [D,CPU_Purchased_ghz],
    [2015-03-19 00:00:00,10.00],
    [2015-03-20 00:00:00,10.00]
]

ie. I want to be able to split it into blocks of 2

share|improve this question
    
you mean split at every other comma? – Touffy Mar 27 at 12:17
    
the built-in split function can almost do it - but how do you differentiate whether a comma starts a new element, or just appends some new parts? You have to write your own logic for that. – doldt Mar 27 at 12:18
    
yeah split every other comma, okay thanks – Vickie Mar 27 at 12:19
    
just to be sure… can you show the full line of JavaScript (generated by PHP if I understand correctly) that creates the original array? because the way you wrote it here, it's an expression with comma operators and the end result would be the number 10 [EDIT: um actually the result would be SyntaxError — anyway, same idea]. – Touffy Mar 27 at 12:29

3 Answers 3

If the variable you're using is actually an array:

var arr1 = [ "D", "CPU_Purchased_ghz", "2015-03-19 00:00:00", 10.00, "2015-03-20 00:00:00", 10.00 ];
var arr2 = [];

for(var i=0, l=arr1.length; i<l; i++){
    arr2.push( [arr1[i], arr1[++i]] );
}

console.log(arr2); // Outputs [ ["D","CPU_Purchased_ghz"], ["2015-03-19 00:00:00",10.00], ["2015-03-20 00:00:00",10.00] ]

JS Fiddle Demo

share|improve this answer
    
When I do alert(arr2) it's still in the original fomat, any idea why this would be? – Vickie Mar 27 at 12:21
    
For debugging, always use console.log, as alert can't directly print an object or array (unless you stringify it). Look at the demo and see if you have something different. – blex Mar 27 at 12:23
    
Oh wait sorry I don't think it is array because I'm converting it from a php array in javascript using var a = <?php echo json_encode($data); ?>; – Vickie Mar 27 at 12:23
    
It's still an array, json_encode should output it in the right format. Check the source code you receive in your browser window to make sure it does. jsfiddle.net/hb2mkq89 – blex Mar 27 at 12:24
    
oh yeah it does! – Vickie Mar 27 at 12:29

This little function will chunk an array into blocks of a specified size:

var chunk = function (chunkSize, array) {
  var chunked = []

  var from

  for (var i = 0, range = array.length / chunkSize; i < range; i++) {
    from = i * chunkSize

    chunked.push(array.slice(from, from + chunkSize))
  }

  return chunked
}

// Usage:
chunk(3, [1, 2, 3, 4, 5])
// -> [[1, 2, 3], [4, 5]]
share|improve this answer
    
+1 for reusability, though the var declaration puzzles me (why not just a function declaration? — or in a more JavaScripty way, adding it to Array.prototype). – Touffy Mar 27 at 12:25
    
Isn't extending native prototypes considered bad practice? And in this situation, function chunk ... would actually be the same, right ... just personal preference (like the missing semicolons). – reg4in Mar 27 at 23:10
    
function chunk would make the function usable before its declaration. Not a big deal, but why write more code? which is also why I don't use semicolons either ;) . As for prototypes: stackoverflow.com/questions/6877005/… – Touffy Mar 28 at 7:55
    
I know about function hoisting ;) And about the prototypes ... meh. I'd say it is preference, too. – reg4in Mar 28 at 16:08

(See Edit for Array solution)

This can be done nicely with a regular expression

var arr = aString.match(/[^,]+,[^,]+/g);

with aString containing your string.

Returns:

[
    "D,CPU_Purchased_ghz", 
    "2015-03-19 00:00:00,10.00", 
    "2015-03-20 00:00:00,10.00"
]

JSFiddle

EDIT: I've just been notified that the variable might be an array and not a string. If that is indeed the case, just join the array first:

var arr = aArray.join(',').match(/[^,]+,[^,]+/g);

JsFiddle for array

share|improve this answer
    
I think OP's input is an array, not a string. Not sure though. – Touffy Mar 27 at 12:19
    
Ah okay, I edited my post – treegarden Mar 27 at 12:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.