1

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

ksbg
  • 3,214
  • 1
  • 22
  • 35
Vickie
  • 89
  • 1
  • 9
  • you mean split at every other comma? – Touffy Mar 27 '15 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 '15 at 12:18
  • yeah split every other comma, okay thanks – Vickie Mar 27 '15 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 '15 at 12:29

3 Answers3

1

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]]
reg4in
  • 564
  • 3
  • 9
  • +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 '15 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 '15 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: http://stackoverflow.com/questions/6877005/extending-object-prototype-javascript – Touffy Mar 28 '15 at 07:55
  • I know about function hoisting ;) And about the prototypes ... meh. I'd say it is preference, too. – reg4in Mar 28 '15 at 16:08
0

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

blex
  • 24,941
  • 5
  • 39
  • 72
  • When I do alert(arr2) it's still in the original fomat, any idea why this would be? – Vickie Mar 27 '15 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 '15 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 = ; – Vickie Mar 27 '15 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. http://jsfiddle.net/hb2mkq89/ – blex Mar 27 '15 at 12:24
0

(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

ksbg
  • 3,214
  • 1
  • 22
  • 35