0

I have a list like this :

[EMP004: BPCE-RNHC-25G8, EMP003: 8FIW-9JRB-NY4J, EMP005: 7QF2-6HI9-XKZZ, EMP002: SG8P-YQKG-ZV3C, EMP001: PBF7-WZHT-WPZR]

What I trying to achieve: User input the first key and then the system will scan through the list and show them corresponding key. For example, if user input "EMP004", it should alert BPCE-RNHC-25G8 for them.

Idea: How can I separate the first key and second key with : ? Other than that how can I know find the corresponding result? Does anyone know how can i solve this with JavaScript only ? Thanks in advance.

WEI ZHUANG GOH
  • 313
  • 1
  • 13
  • 1
    Does this answer your question? [How to create an associative array in JavaScript literal notation](https://stackoverflow.com/questions/37515959/how-to-create-an-associative-array-in-javascript-literal-notation) – Gerard Nov 12 '21 at 08:26

2 Answers2

1

The sample list you have provided is syntactically incorrect. You can create an array object. Then you can create function that takes the key as the parameter like the following way:

const list = [
  { EMP004: 'BPCE-RNHC-25G8' }, 
  { EMP003: '8FIW-9JRB-NY4J' }, 
  { EMP005: '7QF2-6HI9-XKZZ' }, 
  { EMP002: 'SG8P-YQKG-ZV3C' }, 
  { EMP001: 'PBF7-WZHT-WPZR' }
];
const getValByKey = (key) => Object.values(list.find(o => Object.keys(o)[0] === key ))[0];

console.log(getValByKey('EMP004'));
console.log(getValByKey('EMP002'));

Update: For string items you can try the following way:

const list = ['EMP004: BPCE-RNHC-25G8', 'EMP003: 8FIW-9JRB-NY4J', 'EMP005: 7QF2-6HI9-XKZZ', 'EMP002: SG8P-YQKG-ZV3C', 'EMP001: PBF7-WZHT-WPZR'];
const getValByKey = (key) => list.find(item => item.split(':')[0].trim() === key ).split(':')[1].trim();

console.log(getValByKey('EMP004'));
console.log(getValByKey('EMP002'));
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • Thanks for ur reply, but my list format is `[EMP004: BPCE-RNHC-25G8, EMP003: 8FIW-9JRB-NY4J, EMP005: 7QF2-6HI9-XKZZ, EMP002: SG8P-YQKG-ZV3C, EMP001: PBF7-WZHT-WPZR]`. How can i format my current list to your format in order to use this method ? – WEI ZHUANG GOH Nov 12 '21 at 09:06
  • @WEIZHUANGGOH, that list syntax is incorrect, from where you are getting that? if list items are in the form of string (`['EMP004: BPCE-RNHC-25G8', 'EMP003: 8FIW-9JRB-NY4J', 'EMP005: 7QF2-6HI9-XKZZ', 'EMP002: SG8P-YQKG-ZV3C', 'EMP001: PBF7-WZHT-WPZR']`) then only you can format. – Mamun Nov 12 '21 at 09:14
  • Im getting the list from django `view.py`. By this code `mergedCompare = [item_listAssigned[f'subject{i}']+': '+item_listAssigned[f'serial{i }'] for i in range(1, len(item_listAssigned)//2+1)]` – WEI ZHUANG GOH Nov 12 '21 at 09:15
  • @WEIZHUANGGOH, I have added another answer, please check. – Mamun Nov 12 '21 at 09:22
1
You can do like this:

   function search(arr, sub) {
    sub = sub.toLowerCase();
    matchedIndex = arr.map((str, index) => {
      if (
        str.toLowerCase().startsWith(sub.slice(0, Math.max(str.length - 1, 1)))
      ) {
        return index;
      }
    });
    if(matchedIndex.length>0){
        console.log(matchedIndex);
        matchedIndex = matchedIndex.filter(function( element ) {
            return element !== undefined;
         });
       return matchedIndex[0]
    }
  }
  
  var arr = ["EMP004: BPCE-RNHC-25G8", "EMP003: 8FIW-9JRB-NY4J", "EMP005: 7QF2-6HI9-XKZZ", "EMP002: SG8P-YQKG-ZV3C", "EMP001: PBF7-WZHT-WPZR"];
  
  var index = search(arr, 'EMP004');
  if(index>-1){
    matchedvalue=arr[index].split(':');
    console.log(matchedvalue[1]);
  }
  else{
      console.log("Not found")
  }
 
Soshiv Upreti
  • 101
  • 1
  • 6