var found = false;
for (var i = 0; i < some_array.length; i++) {
if (some_array[i].val == some_val) {
found = true;
break;
}
}
if (found)
{
// do something with some_array[i].
}
var found = false;
var key;
for (key in some_hashtable) {
if (some_hashtable[key].val == some_val) {
found = true;
break;
}
}
if (found)
{
// do something with some_hashtable[key].
}
Here the correction of Julian' version:
< script>
var some_array = [1,2,42,33,5,7];
var some_val = 33;
var some_val2 = 42;
var element = some_array.find(function(item) {
return item === some_val;
});
alert(element);
var element2 = some_array.find(item => item === some_val2);
alert(element2);
< /script>
And my improvement for both arrays, hash and index array is this:
Create for the hash array a new array with reverse link from value to key.
Then you can lookup if value occures in your primary hash table and with what key.
The same for indexed array create a hash table for all values and their index numbers,
Both helper hash tables work only with unique values of course or yield tha last stored value key or value index.
PS. for numeric values you have to prefix the hash key value with at least one char. Othervise an index is assigned for nummeric hash keys.
And here my example for the indexed array:
< script>
var some_array = [1,2,42,33,5,7];
var some_val = 33;
var some_val2 = 42;
var some_array_index = [];
for(var i=0;i < some_array.length;i++){
some_array_index["v_"+some_array[i]] = i;
}
if(some_array_index["v_"+some_val]) alert(some_array_index["v_"+some_val]);
if(some_array_index["v_"+some_val2]) alert(some_array_index["v_"+some_val2]);
< /script>
The version for hash arrays looks preaty same : )
here my reverse lookup table for hash array:
< script>
var some_array = [];
some_array["key_a"] = 33;
some_array["key_b"] = 34;
some_array["key_c"] = 42;
var some_array_hash = [];
for(var key in some_array){
some_array_hash["v_"+some_array[key]] = key;
}
var some_val = 33;
var some_val2 = 42;
if(some_array_hash["v_"+some_val]) alert(some_array_hash["v_"+some_val]);
if(some_array_hash["v_"+some_val2]) alert(some_array_hash["v_"+some_val2]);
< /script>
It is not.
You lookup only the value in your array and get the key.
Here your example:
< script>
var some_hashtable = [];
some_hashtable["key_a"] = 33;
some_hashtable["key_b"] = 34;
some_hashtable["key_c"] = 42;
var some_hashtable_hash = [];
for(var key in some_hashtable){
some_hashtable_hash["v_"+some_hashtable[key]] = key;
}
// end of preparation
// now the lookup:
var some_val = 33;
var some_val2 = 42;
if(some_hashtable_hash["v_"+some_val]) alert("element: "+some_val+"\nfound with this key: "+some_hashtable_hash["v_"+some_val]);
if(some_hashtable_hash["v_"+some_val2]) alert("element: "+some_val2+"\nfound with this key: "+some_hashtable_hash["v_"+some_val2]);
< /script>
Here my version:
< script>
var some_hashtable = { a: { value: 111}, b: {value: 222}};
var some_hashtable_lookup = [];
for(var key in some_hashtable){
some_hashtable_lookup["v_"+some_hashtable[key].value] = key;
}
// end of preparation
// now the lookup:
var some_val1 = 111;
var some_val2 = 222;
var some_val3 = 333;
if(some_hashtable_lookup["v_"+some_val1]){alert("element found: "+some_val1+"\nkey is: "+some_hashtable_lookup["v_"+some_val1])} else alert("not found: "+some_val1);
if(some_hashtable_lookup["v_"+some_val2]){alert("element found: "+some_val2+"\nkey is: "+some_hashtable_lookup["v_"+some_val2])} else alert("not found: "+some_val2);
if(some_hashtable_lookup["v_"+some_val3]){alert("element found: "+some_val3+"\nkey is: "+some_hashtable_lookup["v_"+some_val3])} else alert("not found: "+some_val3);
< /script>
Map() das NOT work for reverse lookup of values in the Map.
Take my reverse lookup constructor ;-)
OK, then check this version without lookup table:
< script>
Object.prototype.mySearch = function(val){for(var key in this){if(this[key].value === val)return key}return null}
var some_hashtable = {
a: { value: 111, name: "first"},
b: {value: 222, name: "second"},
c: {value: 333, name: "third"}
};
var myKey = some_hashtable.mySearch(222);
if(myKey) alert(some_hashtable[myKey].name);
< /script>
@ tuchfeld: does it work for you?
Here the correction of Julian' version:What correction are you referring to?
Sorry, please ask Expert Julian.
var key = Object.keys(some_hashtable).find(key => some_hashtable[key].val === some_val);
@ tuchfeld: very good : )
Correct your typo to this and it works:
var key = Object.keys(some_hashtable).find(key => some_hashtable[key].value === some_val);
var some_array = [
{ name: "a", value: 1 },
{ name: "b", value: 2 }
];
var some_value = 2;
var obj = some_array.find(item => item.value === some_value);
alert(obj.name);
//
var some_hashtable = { a: { value: 1 }, b: { value: 2 } };
var key = Object.keys(some_hashtable).find(
key => some_hashtable[key].value === some_value
);
alert(key);
Thank you tuchfeld for the wrap up solution.
I bag for pardon from my side to Expert Julian.
I have changed Julian's correct solution based on first Question example.
Julian has the correct Array elements property .val from the first example which I have removed.
Sorry for confusion.
I would expect the second to be
Open in new window