0

I have a function where several values from a multidimensional array are called very often, like array[idx][3].. In order to save space I want to define variables with these values at the beginning of the function like so:

var na = array[idx][1];
var sc = array[idx][2];
...

I thought it would be faster if it was possible to create an array of variable names -> var tmp = ['na','sc',...];, create a for loop and cycle through the array of names and the multidimensional array, i.e.:

for(i = 0; i = 7, i++){

var tmp[i] = array[idx][i];

}

Is it possible?

3
  • Yes it's possible - looks like you have the right code, what's wrong / what's the question? Commented Aug 7, 2015 at 15:49
  • Is it something that can't be easily tested? Somehow you're overwriting the ['na', 'sc'...] in that for-loop. Commented Aug 7, 2015 at 15:50
  • I don't know, look here jsfiddle.net/dmyyaqe9/2 Commented Aug 7, 2015 at 15:52

1 Answer 1

1
  1. You can use array to store keys.

  2. Then use Object to create a key-> value mapping.

  3. Then use either obj.key(if key is fixed) or obj[key](if key is a calculated value) top get the value.

  4. In your jsfiddle for (i = 0; i = 7; i++) { will hang the browser, you should use i < 7 instead of i = 7.

var keys = ['na', 'sc', 'kd', 'wi', 'ki', 'de', 'sh', 'ti'];
var values = {};

var i, key, len;
// Prevent using magic number 7, get the length from the keys.
for (i = 0, len = keys.length; i < len; i++) {
  values[keys[i]] = i;
}
alert(values.na);

Its possible to further shorten the works by Array.prototype.forEach:

// Create map store.
var v = {};
// Loop through the array
['na', 'sc', 'kd', 'wi', 'ki', 'de', 'sh', 'ti'].forEach(function(key, idx) {
    v[key] = idx;
});
alert(v.na);

jsfiddle

Combine with your idx may be dynamic, you can create a function, given the idx and target array, return the object with the keys.

// A list to simulate what you mentioned with array[idx][i].
var demoList = [
    ['na - 0', 'sc - 0', 'kd - 0', 'wi - 0', 'ki - 0', 'de - 0', 'sh - 0', 'ti - 0'],
    ['na - 1', 'sc - 1', 'kd - 1', 'wi - 1', 'ki - 1', 'de - 1', 'sh - 1', 'ti - 1'],
    ['na - 2', 'sc - 2', 'kd - 2', 'wi - 2', 'ki - 2', 'de - 2', 'sh - 2', 'ti - 2'],
    ['na - 3', 'sc - 3', 'kd - 3', 'wi - 3', 'ki - 3', 'de - 3', 'sh - 3', 'ti - 3'],
    ['na - 4', 'sc - 4', 'kd - 4', 'wi - 4', 'ki - 4', 'de - 4', 'sh - 4', 'ti - 4'],
    ['na - 5', 'sc - 5', 'kd - 5', 'wi - 5', 'ki - 5', 'de - 5', 'sh - 5', 'ti - 5'],
    ['na - 6', 'sc - 6', 'kd - 6', 'wi - 6', 'ki - 6', 'de - 6', 'sh - 6', 'ti - 6'],
    ['na - 7', 'sc - 7', 'kd - 7', 'wi - 7', 'ki - 7', 'de - 7', 'sh - 7', 'ti - 7']
];

var getResult = function(idx, array) {
    var v = {};
    // Only create the mapping if input array and array[idx] are both array.
    if (Array.isArray(array) && Array.isArray(array[idx])) {
        ['na', 'sc', 'kd', 'wi', 'ki', 'de', 'sh', 'ti'].forEach(function(key, i) {
            v[key] = array[idx][i];
        });
    }
    return v;
};

var v = getResult(1, demoList);
alert(v.na);

Sign up to request clarification or add additional context in comments.

2 Comments

Well that's not exactly what i wanted, but i could use i.e. v.na, that's shorter. But it's the same length as an array like v[2], would be easier to define also. Other suggestions?
I'd like to know more precise about what you wanted. values is just a name so it's ok to change to anything you want if its valid. What do you mean 'it's the same length as an array like v[2]'.

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.