We help IT Professionals succeed at work.

improve my javascript code syntax

tuchfeld
tuchfeld asked
on
Hi, following are 2 patterns of javascript code that I frequently use.
I suppose that there is a more "advanced" way to do it.

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].
}

Open in new window


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].
}

Open in new window

Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Use array.find()
The modern way to do it
var element = some_array.find(item => item.val === some_val);

Open in new window


One that is more cross browser
var element = some_array.find(function(item) {
  return item.val === some_val;
});

Open in new window


For a hash table (object) I would use a Map() or Set(). In other words store your values in a Map or Set (depending on use case) and use the methods on those objects to do t he finding for you.
Michel PlungjanIT Expert
Top Expert 2009

Commented:

I would expect the second to be


const found = hashtable[key] && hashtable[key] === some_val;
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
assuming you know the key - in this case he is "iterating" over the Object properties to see if any of them match a value.
ZvonkoSystems architect
Top Expert 2006

Commented:

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>



ZvonkoSystems architect
Top Expert 2006

Commented:

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.



ZvonkoSystems architect
Top Expert 2006

Commented:

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.


ZvonkoSystems architect
Top Expert 2006

Commented:

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>
ZvonkoSystems architect
Top Expert 2006

Commented:

The version for hash arrays looks preaty same  : )

Author

Commented:
Thank You all ! would you please write the code version for the hashtable problem.
ZvonkoSystems architect
Top Expert 2006

Commented:

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>


Author

Commented:
Zvonko thanks but, the hashtable keys format should not be assumed as pre-known as in your code.
ZvonkoSystems architect
Top Expert 2006

Commented:

It is not.

You lookup only the value in your array and get the key.

ZvonkoSystems architect
Top Expert 2006

Commented:

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>

Author

Commented:
this is e.g. for my question:
var some_hashtable = { a: { value: 1}, b: {value: 2}};
I'm looking for the hashtable item that has a specific value.
ZvonkoSystems architect
Top Expert 2006

Commented:

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>

Author

Commented:
I would prefere a solution without lookup. a short and clean solution. I suppose, as Julian said, it should be done using a Map() or Set()
ZvonkoSystems architect
Top Expert 2006

Map() das NOT work for reverse lookup of values in the Map.


Take my reverse lookup constructor  ;-)



ZvonkoSystems architect
Top Expert 2006

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>

ZvonkoSystems architect
Top Expert 2006

@ tuchfeld: does it work for you?

Most Valuable Expert 2017
Distinguished Expert 2018
@Zvonko
Here the correction of Julian' version:
What correction are you referring to?

My code is predicated on an array of objects as specified in the opening question?
@Zvonko, I'm looking for a "modern" one-line statements as Julian solved it for the array (the first answer).
ZvonkoSystems architect
Top Expert 2006

Sorry, please ask Expert Julian.

ok, I think, this is what I was looking for the hashtable part:
var key = Object.keys(some_hashtable).find(key => some_hashtable[key].val === some_val);

Open in new window

Thank you all for the replies and care!
ZvonkoSystems architect
Top Expert 2006

@ 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);


@Zvonko, so let's write the full answer (tested):

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);

Open in new window

ZvonkoSystems architect
Top Expert 2006

Thank you tuchfeld for the wrap up solution.


ZvonkoSystems architect
Top Expert 2006

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.