0

I have this array:

var skins = {};
skins.w = "http://example.com/public/images/char_elements/base.png";

var eyes = {};
eyes.b = "http://example.com/public/images/char_elements/eyes/blue.png";
eyes.g = "http://example.com/public/images/char_elements/eyes/green.png";
eyes.r = "http://example.com/public/images/char_elements/eyes/red.png";

var hair = {};
hair.b = "http://example.com/public/images/char_elements/hair/black.png";
hair.w = "http://example.com/public/images/char_elements/hair/blond.png";
hair.s = "http://example.com/public/images/char_elements/hair/brown.png";

var mouth = {};
mouth.h = "http://example.com/public/images/char_elements/mouth/happy.png";

var pants = {};
pants.s = "http://example.com/public/images/char_elements/pants/shorts.png";

var shoes = {};
shoes.b = "http://example.com/public/images/char_elements/shoes/black.png";

var torso = {};
torso.s = "http://example.com/public/images/char_elements/torso/shirt.png";

And this function:

function SwitchElement(type){
switch(type){

    case "eyes":
    var cur = eyes.indexOf(document.getElementById(type).src);
    console.log(cur);
//  var eyesId = document.getElementById("
    break;
}
}

And all this is called by:

<img class="builder_control_front" src="http://claro.mib.infn.it/arrow.png" onclick="SwitchElement('eyes');"/>

So basically when user clicks on arrow, it calls javascript function which gets current image loaded, get's index of array where element is located (in this case array: "eyes") and cycle through it.

I need to get index by url, when I try to use indexOf it throws Undefined, I read that JavaScript will convert array to Object if array has named indexes.

So chow can I get index of current loaded image?

Note: I NEED to have array with codes like: b, g, r, w, w4, b3 etc.. so Changing to normal array is not quite an option, perhaps changing to multidimensional array would be better?

4
  • you could make an object of objects { eyes: { w: ... } } Commented Feb 20, 2015 at 20:56
  • Objects in JS don't have an index Commented Feb 20, 2015 at 20:57
  • you'd probably want to loop through the eyes like stackoverflow.com/questions/19969751/… and do something like for( var i in eyes ) { if (eyes[i] == document.getElementById(type).src) { cur = i; break; } } or something like that Commented Feb 20, 2015 at 21:03
  • " I read that JavaScript will convert array to Object if array has named indexes." Arrays are objects. They can have named properties, but only the numerical indices correspond to the array elements. Commented Feb 20, 2015 at 21:08

2 Answers 2

0

Why not just iterate your object and find the key containing the value:

function findKey(obj, value) {
    for (var key in obj) {
        if (obj[key] == value) return key
    }
    return null;
}

var key = findKey(eyes, document.getElementById(type).src);
if (key != null) { 

} else {

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

Comments

0

What you need is a reverse lookup. Assuming that all of your URLs are unique, you can create one like this:

function invert(obj) {
    var newObj = {};
    for(var key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[obj[key]] = key;
        }
    }
}

var hair = {};
hair.b = "http://example.com/public/images/char_elements/hair/black.png";
hair.w = "http://example.com/public/images/char_elements/hair/blond.png";
hair.s = "http://example.com/public/images/char_elements/hair/brown.png";
var hairReverse = invert(hair);

var url = "http://example.com/public/images/char_elements/hair/black.png";
console.log(hairReverse[url]); // logs "b"

If you're using Lodash/Underscore, it has an invert function built in:

var hairReverse = _.invert(hair);

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.