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,951
  • 10
  • 65
  • 112
Rym
  • 650
  • 4
  • 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

6

New in JavaScript 1.7 - Ignoring some returned values:

var [delta, newvalue, admin] = resp.split(':');
flesk
  • 7,439
  • 4
  • 24
  • 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,584
  • 3
  • 28
  • 42
  • 1
    Agreed, this is really the only improvement that can be made, though I don't think it's what the OP intended. Note that you can also lose the second `var`. – lonesomeday Dec 14 '11 at 11:31
  • Doesn't that push newvalue and admin into global scope? – Rym Dec 14 '11 at 11:34
  • 3
    Nope, notice that there is no ; after the first line - it means that you continue declaring variables and can omit var on each line. – Andrey Selitsky Dec 14 '11 at 11:36
  • Yeah, not sure if that's what the OP was looking for, but okay. – Jason Dec 14 '11 at 11:47
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
  • 147,572
  • 30
  • 200
  • 241
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
  • 28,903
  • 6
  • 107
  • 121
  • 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
  • 3,379
  • 25
  • 32
  • 2
    I'd suggest making `output` an optional third parameter so that you can choose whether to extend/update an existing option or create a new one. – nnnnnn Dec 14 '11 at 11:51
  • That would be encompassing too much logic into the function. Write an extend function for that and just wrap it around. – Jason Dec 14 '11 at 11:53
  • Too much? One more line: `if (!output) output = {};` – nnnnnn Dec 14 '11 at 11:58
  • No, it's not too much more "code." Mind you, I come from a C background, so these things are non-sequitur and are generally separated. Either would work. – Jason Dec 14 '11 at 12:00
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,582
  • 3
  • 29
  • 42
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
  • 11,033
  • 4
  • 35
  • 50
  • 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