-1

I'm a sudent starting to learn Javascript. We're working with arrays. The question I have is when working with two arrays, how can I match indexes when one index is chosen. Example:

var nameArray = [ "Earl", "John", "Shelia", "Lenny" ];

var testScoreArray = [ 85, 92, 87, 99 ];

If you chose Lenny, I want to be able to match his score with his name and vice versa. The arrays aren't fixed and are part of a program that can add names and scores at any time. I understand you can program it to store the names and test scores as a multi-dimensional array but not for this example.

I would like to be able to look a score up based on it being the lowest, highest or another situation and match the score with the name and vise versa.

2
  • 1
    Can you add a score without a name associated with it? Can there be a name without a score? Commented Jun 12, 2015 at 19:42
  • 1
    just use that index on the other array? array2[index]? if you can't assume they're in the same position it's not possible they way you're trying to do it Commented Jun 12, 2015 at 19:44

5 Answers 5

2

Given this:

var array1 = ['Earl', 'John', 'Shelia', 'Lenny']

… you can get the index of "Lenny" like this:

array1.indexOf('Lenny');  //3

So you simply need to plug that into the other array:

array2[array1.indexOf('Lenny')] //99

Snippet

var array1 = ['Earl', 'John', 'Shelia', 'Lenny'],
    array2 = [85,92,87,99]

console.log(array2[array1.indexOf('Lenny')]); //99

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

1 Comment

Thanks for all the replies, I'm trying the suggestions to see if I can get it to work.
1

The data structure that you are looking for is a bidirectional map. Description from Wikipedia:

In computer science, a bidirectional map, or hash bag, is an associative data structure in which the (key, value) pairs form a one-to-one correspondence. Thus the binary relation is functional in each direction: value can also act as a key to key. A pair (a, b) thus provides a unique coupling between a and b so that b can be found when a is used as a key and a can be found when b is used as a key.

Mathematically, a bidirectional map between keys and values is equivalent to a pair of functions:

  1. A function f from keys to values (i.e. a -> b).
  2. A function g from values to keys (i.e. b -> a).

In addition, there are two constraints imposed on bidirectional maps:

  1. Given any key x the constraint g (f x) = x must be satisfied.
  2. Given any value y the constraint f (g y) = y must be satisfied.

Therefore, Bimap(a, b) = (a -> b, b -> a).

Alternatively, a bidirectional map can be represented as a pair of maps or associative arrays:

Since Map(a, b) = a -> b, therefore Bimap(a, b) = (Map(a, b), Map(b, a)).

Internally, an associative array could be represented as either a pair of lists (i.e. ([a], [b])) or a list of pairs (i.e. [(a, b)]) or hash tables or any other suitable data structure.

Let's implement a bidirectional map data structure using hash tables internally for efficiency:

function Bimap(keys, vals) {
    var length = Math.min(keys.length, vals.length);

    var valMap = this.lookupVal = Object.create(null);
    var keyMap = this.lookupKey = Object.create(null);

    for (var i = 0; i < length; i++) {
        var key = keys[i];
        var val = vals[i];

        valMap[key] = val;
        keyMap[val] = key;
    }
}

You can use it as follows:

var bimap = new Bimap(["Earl", "John", "Shelia", "Lenny"], [85, 92, 87, 99]);

bimap.lookupVal["Lenny"]; // 99
bimap.lookupKey[99]; // "Lenny"

You can also add new key value pairs to the bidirectional map as follows:

Bimap.prototype.insert = function (keys, vals) {
    var length = Math.min(keys.length, vals.length);

    var valMap = this.lookupVal;
    var keyMap = this.lookupKey;

    for (var i = 0; i < length; i++) {
        var key = keys[i];
        var val = vals[i];

        valMap[key] = val;
        keyMap[val] = key;
    }
};

You would use it as follows:

bimap.insert(["Jane"], [96]);

bimap.lookupVal["Jane"]; // 96
bimap.lookupKey[96]; // "Jane"

You can summarize bidirectional maps (such as finding min/max values, etc.) using reduce:

Bimap.prototype.reduceVals = function (inductive, basis) {
    var valMap = this.lookupVal, undefined;

    return basis === undefined ?
        Object.keys(valMap).reduce(callback) :
        Object.keys(valMap).reduce(callback, basis);

    function callback(proof, key) {
        return inductive(proof, valMap[key]);
    }
};

For example:

bimap.lookupKey[bimap.reduceVals(Math.min)]; // "Earl"
bimap.lookupKey[bimap.reduceVals(Math.max)]; // "Lenny"

Finally, most JavaScript engines now natively support the Map data structure. Hence, you can implement Bimap in terms of that instead.

Comments

0

You can create object of array in javascript like this

[{Name : "Test" , Score : 25},{Name : "Test Two" , Score : 35}]

1 Comment

He did say using two arrays {...}
0

The best way to do this is by using either associative arrays or multi-dimensional arrays. I would suggest taking a look at this tutorial: www.w3schools.com/js/js_arrays.asp

1 Comment

He said he needs to use 2 arrays
0

If I understand your question correctly, you want to be able to match a name with a score in two different arrays. To do this, let's say they give you Lenny, first by using a for loop, you can loop through the first array, and look for his name, while keeping count on which index you're on. The code would look like

int index = 0;
for(; index < array1.size(); index++)

When you find his name, just use that index that you found like array2[index] and you'll have your answer.

Comments

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.