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];
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];
New in JavaScript 1.7 - Ignoring some returned values:
var [delta, newvalue, admin] = resp.split(':');
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];
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.)
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();
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
use for loop
var values = resp.split(':');
for(var i=0; i<values.length; i++) {
var value = values[i];
alert(i =") "+value);
}
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.