newman
newman

Reputation: 421

Is it possible to check the condition of a value of an object and JUST return the key?

I have an object of letters and numbers inside of a function. This function takes in an array of numbers and I'm running a for in loop that iterates over the object and checks a condition. If any of the numbers in the array match any of the values in the object, return just the key to that value.

So If I pass in switcher(['26']), it should return 'a'. Is this possible?

function switcher(x){
const letters = {
  a: '26',
  b: '25',
  c: '24',
  d: '23',
  e: '22',
  f: '21',
  g: '20',
  h: '19',
  i: '18',
  j: '17',
  k: '16',
  l: '15',
  m: '14',
  n: '13',
  o: '12',
  p: '11',
  q: '10',
  r: '9',
  s: '8',
  t: '7',
  u: '6',
  v: '5',
  w: '4',
  x: '3',
  y: '2',
  z: '1'
};
}

I have attempted to do this via the ES6 map() method, but I am unsure as to what to put in my if statement.. Here is what I have so far:

return x.map(function(number){
  let keys = Object.keys(letters);
  for(var key in letters){
    if(letters[key] === number){
    }
  }
 });
}

Is there an easier way to do this?

Upvotes: 0

Views: 78

Answers (6)

Bergi
Bergi

Reputation: 664970

You don't even need that lookup table letters (which is the wrong way round for your purpose anyway). Just use charCodeAt and fromCharCode for converting letters to numbers and back:

function switcher(x) {
   return x.map(function(code) {
       return x > 0 && x <= 26 ? String.fromCharCode(123-x) : "";
   });
}
console.log(switcher([26, 1])); // ["a", "z"]

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237975

I'm going to presume from the fact that you are searching using an array that you may want more than one result. For example, ["26", "5"] should return ["a", "v"].

You can do this with a one-liner:

const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'};

const search = ["26", "5"];
const results = Object.entries(letters).filter(l => search.includes(l[1])).map(l => l[0]);

console.log(results);

This is post-ES6 code. (Array#includes is ES2016; Object.entries is ES2017.) It's easily polyfillable, however.

Object.entries makes an array where each property of the object is an element in the array in the format [key, value], such as ["a", "26"]. We then filter them by testing to see if our search array includes the element. We then use Array#map to get just the second element from each array, i.e. the values from the original array.

Upvotes: 0

Redu
Redu

Reputation: 26191

One might also do as;

function switcher(x){
var letters = {
    a: '26',
    b: '25',
    c: '24',
    d: '23',
    e: '22',
    f: '21',
    g: '20',
    h: '19',
    i: '18',
    j: '17',
    k: '16',
    l: '15',
    m: '14',
    n: '13',
    o: '12',
    p: '11',
    q: '10',
    r: '9',
    s: '8',
    t: '7',
    u: '6',
    v: '5',
    w: '4',
    x: '3',
    y: '2',
    z: '1'
  };
    for (var k in letters) if (letters[k] === x) return k;
    return undefined;
}

console.log(switcher("14"));

Upvotes: 1

Barmar
Barmar

Reputation: 781741

You can simply use return key;

function switcher(x) {
  const letters = {
    a: '26',
    b: '25',
    c: '24',
    d: '23',
    e: '22',
    f: '21',
    g: '20',
    h: '19',
    i: '18',
    j: '17',
    k: '16',
    l: '15',
    m: '14',
    n: '13',
    o: '12',
    p: '11',
    q: '10',
    r: '9',
    s: '8',
    t: '7',
    u: '6',
    v: '5',
    w: '4',
    x: '3',
    y: '2',
    z: '1'
  };
  return x.map(function(number) {
    let keys = Object.keys(letters);
    for (var key in letters) {
      if (letters[key] === number) {
        return key;
      }
    }
  });
}
console.log(switcher(['26']));

But if you're going to do this frequently, the other answers with the inverted object are better.

Upvotes: 1

trincot
trincot

Reputation: 350760

I would suggest to just swap the key/value pairs, and work with that.

If you want the code to make the swap, you can do it in a one-shot operation (assigned to numbers Map). This is ES6 code:

const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'};
const numbers = new Map(Object.keys(letters).map( k => ([letters[k], k])));

console.log(numbers.get('26'));
console.log(numbers.get('9'));

Upvotes: 2

kind user
kind user

Reputation: 41893

You could use Object.keys and Array#find to get the key of the matched value.

const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'};

function switcher(num){
  var res = Object.keys(letters).find(v => letters[v] == num);
  return res;
}

console.log(switcher('26'));
console.log(switcher('9'));

Upvotes: 3

Related Questions