1

I have a string

string s='2011/01/03 00:00:00,14.00|2011/01/03 00:00:00,14.50|2011/01/03 00:00:00",15.00|2011/01/02 00:00:00,12.00|';

I want to split the string first by '|' , which i can do by using

var ds=[];
 ds=s.split('|');

i further want to split them by ',' and store in an array

The question is how do i store in a multidimention array so the output looks similar to this

da[0]={'2011/01/03 00:00:00',14.00}
da[1]={'2011/01/03 00:00:00',14.50}
.
.
.

Thanks Prady

2
  • If you really want a multi-dimensional array, see @patrickdw's answer for what the syntax looks like. Curly braces are for object literals (key/value pairs), not arrays (ordered values). Commented Jan 14, 2011 at 3:56
  • thanks Phrogz and patrick.. I was looking for an multidimentional array,, I had made a mistake by using the } instead of ] Commented Jan 14, 2011 at 4:08

2 Answers 2

2
var ds = s.split('|');

for( var i = 0, len = ds.length; i < len; i++ ) {
    if( ds[i] )
        ds[i] = ds[i].split(',');
}

Result looks like:

[
  ['2011/01/03 00:00:00','14.00'],
  ['2011/01/03 00:00:00','14.50'],
  ['2011/01/03 00:00:00','15.00'],
  ['2011/01/02 00:00:00','12.00'],
  ''
]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for caching the length of the array, but -1 for multi-line statement without curly braces. :p
2
var s='2011/01/03 00:00:00,14.00|2011/01/03 00:00:00,14.50|2011/01/03 00:00:00,15.00|2011/01/02 00:00:00,12.00';
var pieces = s.split('|');
for (var i=0,len=pieces.length;i<len;++i){
  var pair = pieces[i].split(',');
  pieces[i] = {};
  pieces[i][pair[0]] = pair[1]*1; //*1 to convert from string to number
}

console.log(pieces);
// [
//   {'2011/01/03 00:00:00':14.00},
//   {'2011/01/03 00:00:00':14.50},
//   {'2011/01/03 00:00:00':15.00},
//   {'2011/01/02 00:00:00':12.00},
// ]

This is assuming, based on your pseudo-JS syntax using curly braces, that you really meant an array of objects instead of a multi-dimensional array. If you want a single object hashing 'time-as-string' to values, you might do:

var pieces = s.split('|');
var values = {};
for (var i=0,len=pieces.length;i<len;++i){
  var pair = pieces[i].split(',');
  values[pair[0]] = pair[1]*1;
}
console.log(values);
// {
//   '2011/01/03 00:00:00': 14.00,
//   '2011/01/03 00:00:00': 14.50,
//   '2011/01/03 00:00:00': 15.00,
//   '2011/01/02 00:00:00': 12.00,
// }

This would allow you to find the value for any given day in constant time, without traversing the array of values.

2 Comments

I see you opted to interpret it as objects. You may be right. I'll give a +1 for that. :o) Oh, and for the number conversion as well. Good catch.
@patrickdw I suspect I'm not right; it makes little sense to have an array of objects with a single key each. I'll edit to add another possible guess, but I think you were right to go with the asked-for multidimensional array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.