是否可以检查对象值的条件并返回密钥?

时间:2023-01-01 12:42:23

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.

我在函数内部有一个字母和数字对象。这个函数接受一个数字数组,我正在运行for循环,迭代对象并检查条件。如果数组中的任何数字与对象中的任何值匹配,则只返回该值的键。

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

所以如果我传入切换器(['26']),它应该返回'a'。这可能吗?

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:

我试图通过ES6 map()方法做到这一点,但我不确定在if语句中放入什么..这是我到目前为止所拥有的:

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?

有更简单的方法吗?

6 个解决方案

#1


3  

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

您可以使用Object.keys和Array#find来获取匹配值的键。

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

#2


2  

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:

如果您希望代码进行交换,您可以在一次性操作中执行此操作(分配给数字Map)。这是ES6代码:

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

#3


1  

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.

但是如果你经常这样做,那么倒置物体的其他答案会更好。

#4


1  

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

#5


0  

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

我将假设你正在使用一个你可能需要多个结果的数组进行搜索。例如,[“26”,“5”]应返回[“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.

这是ES6后的代码。 (Array#includes是ES2016; Object.entries是ES2017。)然而,它很容易可以填充。

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.

Object.entries创建一个数组,其中对象的每个属性都是数组中的元素,格式为[key,value],例如[“a”,“26”]。然后,我们通过测试来过滤它们,看看我们的搜索数组是否包含该元素。然后,我们使用Array#map从每个数组中获取第二个元素,即原始数组中的值。

#6


0  

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:

你甚至不需要那些查找表字母(无论如何这是错误的方式)。只需使用charCodeAt和fromCharCode将字母转换为数字并返回:

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

#1


3  

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

您可以使用Object.keys和Array#find来获取匹配值的键。

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

#2


2  

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:

如果您希望代码进行交换,您可以在一次性操作中执行此操作(分配给数字Map)。这是ES6代码:

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

#3


1  

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.

但是如果你经常这样做,那么倒置物体的其他答案会更好。

#4


1  

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

#5


0  

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

我将假设你正在使用一个你可能需要多个结果的数组进行搜索。例如,[“26”,“5”]应返回[“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.

这是ES6后的代码。 (Array#includes是ES2016; Object.entries是ES2017。)然而,它很容易可以填充。

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.

Object.entries创建一个数组,其中对象的每个属性都是数组中的元素,格式为[key,value],例如[“a”,“26”]。然后,我们通过测试来过滤它们,看看我们的搜索数组是否包含该元素。然后,我们使用Array#map从每个数组中获取第二个元素,即原始数组中的值。

#6


0  

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:

你甚至不需要那些查找表字母(无论如何这是错误的方式)。只需使用charCodeAt和fromCharCode将字母转换为数字并返回:

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