1

I have a bit of a problem with arrays.

I have a string as value, which is unique to an array index, e.g. "daa12d956752gja2":

g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";

This string is what I know. What I need to get is the index, so "87686479/welcome.html". The thing is... I have dozens of arrays like this. It basically looks like this:

g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";

...

I tried the indexOf method, but it doesn‘t seem to find the array index for the value after the equation sign.

I can‘t change the array unfortunately.

Help is much appreciated. Sorry for the formatting, I'm on mobile.

5
  • and will cause a SyntaxError in Javascript. You might consider using ' instead. Commented Jan 7, 2019 at 8:38
  • do you have really an array instead of an object? Commented Jan 7, 2019 at 8:40
  • I think so... If I type in g_nodeMapping into the browser console it says „object array“ Commented Jan 7, 2019 at 8:43
  • try instead array object : g_nodeMapping = []; g_nodeMapping = {}; Commented Jan 7, 2019 at 8:43
  • As stated above, I can‘t change the array unfortunately :( Commented Jan 7, 2019 at 8:45

2 Answers 2

1

You can define a function findCustomKey that takes an array element (or value) as a parameter and returns the key. The following example shows that:

var arr = [];
arr["8374628/test.html"] = "489fa3682975da";
arr["8953628/anothersite.html"] = "gi764295hf46";
arr["267857543/helpplx.html"] = "8653468te87a";

function findCustomKey(ele) {
    let keys = Object.keys(arr);
    for (let keyEle of keys) {
        if (arr[keyEle] == ele) {
            return keyEle;
        }
    }
}

console.log(findCustomKey("489fa3682975da"));
console.log(findCustomKey("8653468te87a"));
console.log(findCustomKey("abcd123"));


The Output:

8374628/test.html
267857543/helpplx.html
undefined


Another Version (EDIT added):

Here is another way to code findCustomKey(the way to use it remains same):

function findCustomKeyV2(ele) {
    return Object.keys(arr).filter(k => arr[k] == ele)[0];
}


Another Version:

This version of the solution was added as the above code did not work on IE browsers. The following code worked on Firefox, Chrome and IE11 browsers.

var arr = [];
arr['8374628/test.html'] = '489fa3682975da';
arr['8953628/anothersite.html'] = 'gi764295hf46';
arr['267857543/helpplx.html'] = '8653468te87a';

var arrMap = new Map();
for (let k in arr) {
    arrMap.set(arr[k], k);
}

console.log(arrMap.get('489fa3682975da'));
console.log(arrMap.get('8653468te87a'));
console.log(arrMap.get('abcd123'));
Sign up to request clarification or add additional context in comments.

3 Comments

It works on mozilla and chrome, but I get a SCRIPT5009 error in IE11 with edge compatibility. Any ideas why?
I had tried on Firfox and Chrome with no issues. I tried on the IE 11- I get the error SCRIPT1002: Syntax error. I searched the net (Google). There seems to no specific consistent reason, except the feature may not have been supported on the IE browser. There are various solutions in different contexts - I couldn't figure what the real issue is. If I find a proper reason I will post here.
@schokogabel332 I added alternative solution that worked on IE11 also. Please try and let me know - if possible.
1

You could find the key by getting all keys from the object/array and find the value.

function getKey(object, value) {
    return Object.keys(object).find(k => object[k] === value);
}
   
var g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";
g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";

console.log(getKey(g_nodeMapping, "daa12d956752gja2"));

1 Comment

Thank you, I tried the other answer and it works. Thank you vers much for your solution.

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.