简体   繁体   English

使用Javascript数组将键和值与某个变量匹配的正确方法是什么?

[英]What is the proper way to match key and value to some variable using Javascript array?

I'm trying to create a function in javascript in order to get a value that match an external variable. 我试图在javascript中创建一个函数,以获取与外部变量匹配的值。

I created an array of key/value. 我创建了一个键/值数组。 Then I made a function that will check if a value match to a variable. 然后,我制作了一个函数,该函数将检查值是否与变量匹配。 Then i will return the key of this value. 然后,我将返回此值的密钥。

function findNumberFromMonth($data)
{    
var obj = {
    01: "Jan",
    02: "Feb",
    03: "Mar",
    04: "Apr",
    05: "May",
    06: "Jun",
    07: "Jul",
    08: "Aug",
    09: "Sep",
    10: "Oct",
    11: "Nov",
    12: "Dec"
    }

 for(var element in obj)
         {
             if($data == element.value )
                 {
                     $number = element.key
                 }
          return $number;
         }

}

I expected the output of "01" if i initialize $data as "Jan". 如果我将$ data初始化为“ Jan”,我期望输出为“ 01”。

But i only got an undefined object. 但是我只有一个未定义的对象。 How may i process ? 我该如何处理?

Thank you for your replies. 谢谢您的回复。

If what you want to do is map month names to month numbers, your object is backwards: 如果您要做的是将月份名称映射到月份编号,则对象向后:

function findNumberFromMonth($data)
{    
   var months = {
    "Jan": "01",
    "Feb": "02",
    "Mar": "03",
    // ...
    };

    return months[$data];
}

you can do this. 你可以这样做。

 var obj = { 01: "Jan", 02: "Feb", 03: "Mar", 04: "Apr", 05: "May", 06: "Jun", 07: "Jul", 08: "Aug", 09: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" } var key = Object.keys(obj)[Object.values(obj).indexOf("Jun")] console.log(key) 

This can be done with find and Object.entries : 这可以通过findObject.entries

 function findNumberFromMonth(data) { const months = { 01: "Jan", 02: "Feb", 03: "Mar", 04: "Apr", 05: "May", 06: "Jun", 07: "Jul", 08: "Aug", 09: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" }; return Object.entries(months).find(([key, val]) => data === val)[0]; } console.log(findNumberFromMonth('Oct')); 

When you use for(let x in obj) x is the key of the object. 当使用for(let x in obj) x for(let x in obj) x是对象的key for...in. 为...英寸

 function findNumberFromMonth($data) { var obj = { 01: "Jan", 02: "Feb", 03: "Mar", 04: "Apr", 05: "May", 06: "Jun", 07: "Jul", 08: "Aug", 09: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" } for(var key in obj) { if($data == obj[key]) return key; } } console.log(findNumberFromMonth('Jan')); 

if you need both keys and values simultaneously, use object.entries and iterate over it. 如果同时需要键和值,请使用object.entries并对其进行迭代。

  const data = "Feb"; const obj = { "01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr", "05": "May", "06": "Jun", "07": "Jul", "08": "Aug", } for (let [key, value] of Object.entries(obj)) { if(data === value ) { console.log(key); } } 

You assign $number variable in if statement. 您在if语句中分配$ number变量。 But return it in out of if statement. 但是从if语句中返回它。 You must move return line to if statement and if not returned anything, you can return false value. 您必须将返回行移至if语句,如果未返回任何内容,则可以返回false值。

function findNumberFromMonth($data) {
  var obj = {
    01: "Jan",
    02: "Feb",
    03: "Mar",
    04: "Apr",
    05: "May",
    06: "Jun",
    07: "Jul",
    08: "Aug",
    09: "Sep",
    10: "Oct",
    11: "Nov",
    12: "Dec"
  }

  for (var key in obj) {
    if ($data === obj[key])
      return key;
  }

  return false;

}

console.log(findNumberFromMonth('Mar'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象数组中匹配键和值并返回仅具有该键的新 object 的正确方法是什么 - What is the proper way to match key and value from an array of objects and return a new object with only that key 使用查找索引从数组中获取 object 或在 JavaScript 中的 map 的键处获取值的更快方法是什么? - What is a faster way to get an object from array using find index, or getting value at a key of a map in JavaScript? 使用CSS和javascript用一些具有不同样式的单词制作段落的正确方法是什么 - What is the proper way to make a paragraph with some words having different styles using CSS and javascript Javascript数组键值作为变量 - Javascript array key value as a variable 使用 Javascript 有没有办法将信息推送到键/值对之外的数组中? - Using Javascript is there a way to push information into an array that is apart of a key/value pair? 删除打字稿中的键值对的正确方法是什么 - What is the proper way to remove a key-value pair in typescript 确定值是否与jqGrid数据中的任何键匹配的正确方法是什么? - What is the proper way to identify if a value matches any key in the data of a jqGrid? map integer 到 Javascript 中其他变量的正确方法是什么 - What is proper way to map integer to other variable in Javascript Javascript / jQuery变量,使用var的正确方法是什么? - Javascript/jQuery variable, what is the proper way to use a var? 定义javascript变量的正确方法? - Proper way of defining a javascript variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM