2

Just feels like there's probably a nicer way.

var values = resp.split(':');
var delta = values[0]; var newvalue = values[1]; var admin = values[2];
alain.janinm
  • 19,035
  • 10
  • 56
  • 103
Rym
  • 650
  • 3
  • 16
  • 6
    That's called [desructuring assignment][1] and it's not particularly standards-compliant. [1]: http://stackoverflow.com/questions/3986348/multiple-assignment-in-javascript – Anton Gogolev Dec 14 '11 at 11:30
  • That's pretty rad :D.. pity it's not fully complient :( – Rym Dec 14 '11 at 11:35

7 Answers7

5

New in JavaScript 1.7 - Ignoring some returned values:

var [delta, newvalue, admin] = resp.split(':');
flesk
  • 7,046
  • 3
  • 19
  • 33
3

I can only think of:

var values = resp.split(':');
var delta = values[0], 
    newvalue = values[1],
    admin = values[2];

or as lonesomeday suggested:

var values = resp.split(':'),
    delta = values[0],
    newvalue = values[1],
    admin = values[2];
Andrey Selitsky
  • 2,579
  • 3
  • 26
  • 41
2

In my opinion it's nicer not to repeat the var keyword. So:

var values   = resp.split(':'),
    delta    = values[0],
    newvalue = values[1],
    admin    = values[2];

(With or without the linebreaks after the commas, and with or without aligning the equals signs.)

But there's little else you can do to pretty it up.

Unless you're willing to do something silly using a helper function like this:

function assignFromArray(vars, vals){
  for (var i=0; i<vars.length; i++)
     window[vars[i]] = vals[i];
}

assignFromArray(["delta","newValue","admin"], resp.split(":"));

(Which of course only works for global variables, though it would be easy enough to create properties on some local object for non-global scope.)

nnnnnn
  • 138,378
  • 23
  • 180
  • 229
2

You could avoid using an explicit index, and just order the declarations accordingly. Could of course also be done with a single var keyword.

It is easier to change and expand, since you probably want to have the declaration in the order of appearance in the array anyway.

var values = resp.split(':');
var delta    = values.shift(); 
var newvalue = values.shift();
var admin    = values.shift();

With a single statement:

var values = resp.split(':'),
    delta    = values.shift(), 
    newvalue = values.shift(),
    admin    = values.shift();
kapex
  • 26,163
  • 5
  • 97
  • 111
  • 2
    +1. I think this is the nicest variation, though I'd prefer it with the suggested single `var` statement. – nnnnnn Dec 14 '11 at 11:47
  • 1
    @lonesomeday - In this case we know the array only holds three values though, so the speed (or lack thereof) is not going to be noticed by the user even if later extended to ten values, or twenty. (Though I assume it would be faster to assign the variables in reverse order and use `.pop()`?) – nnnnnn Dec 15 '11 at 20:46
  • Very true. And if you need to extract data into twenty different variables, you have a bigger problem! – lonesomeday Dec 15 '11 at 21:02
1

Use a nice little assign() function to associate values with keys in a new object. Then, the object is returned:

var assign = function(values, keys) {
    var i, vmax, kmax, output = {};
    for(i=0, vmax=values.length, kmax=keys.length; i<vmax && i<kmax; i++) {
        output[keys[i]] = values[i];
    }

    return output;
};

var result = assign(["bar","beer"], ["foo","free"]);

console.log(result);
// returns { "foo" : "bar", "free" : "beer" } accessed as result.foo and result.free
Jason T Featheringham
  • 3,188
  • 23
  • 32
0

use for loop

var values = resp.split(':');

for(var i=0; i<values.length; i++) {
    var value = values[i];
    alert(i =") "+value);
}
Pradeep Singh
  • 3,424
  • 3
  • 26
  • 41
0

To get rid of the array, you could try something like this:

var delta = resp.substring(0, resp.indexOf(':')),
    newvalue = resp.substring(resp.indexOf(':') + 1, resp.lastIndexOf(':')),
    admin = resp.substring(resp.lastIndexOf(':') + 1, resp.length);

Don't worry, I'm not expecting an accepted answer! :) Just trying to present another way of looking at things.

Nathan MacInnes
  • 10,685
  • 4
  • 33
  • 47
  • Alternative viewpoints are always good, but this isn't going to cope too well if a fourth variable is introduced... – nnnnnn Dec 14 '11 at 11:55
  • Yes, I did think of that. The only way would be to do `resp = resp.substring(resp.indexOf(':'));` after each assignment, but then it'd just get messy. Still, I have found `substring`/`substr` combined with `indexOf` very useful in slightly different situations in the past where I don't want to resort to `RegExp`. – Nathan MacInnes Dec 14 '11 at 13:31
  • Also, this is by far the quickest method here... `split` is really slow, I assume because it converts the string into a RegExp before processing. – Nathan MacInnes Dec 15 '11 at 10:39
  • 1
    To adapt for more values in the string you don't need to keep changing `resp` each time: `.indexOf()` takes an optional second parameter that is the index to start searching from, so add a temporary variable to store the index (which would also avoid your current repeated call to `.indexOf()` to find the _same_ value), and then the next call can be `resp.indexOf(":",prevIndex+1)`. As for the speed of `.split()` - I wouldn't worry unless I actually noticed the page lagging, because the `.split()`-based code is a lot clearer. Splitting a string into 3 (or 10, or 20) pieces isn't noticeably slow. – nnnnnn Dec 15 '11 at 20:41
  • Oh yes, I'd forgotten about that second argument. And no, split isn't noticeably slow, even though indexOf is several times faster... it's purely academic :) – Nathan MacInnes Dec 15 '11 at 23:15