是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)

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

所以如果我传入switcher(['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'
};
}

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

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

有更简单的方法吗?


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?


原文:https://stackoverflow.com/questions/43242342
2024-05-06 17:05

满意答案

使用dom元素中的data

$(".test").focusout(function(){
     var qtdCont = parseInt($(this).val());
    if(qtdCont > 0 && $(this).data('done') == undefined)
    {
         var qtdProd     = $(".value").val();
         var qtdProdInt  = parseInt(qtdProd);
         var qtdProdTot  = qtdProd-qtdCont;
         $(".value").val(qtdProdTot);
        $(this).data('done', true);
     }
});

小提琴: http : //jsfiddle.net/dNEmD/19/

UPDATE

$(".test").focusout(function(){
     var qtdCont = parseInt($(this).val());
    if(qtdCont > 0 && 
         ($(this).data('done') == undefined || $(this).data('done') == false))
    {
         var qtdProd     = $(".value").val();
         var qtdProdInt  = parseInt(qtdProd);
         var qtdProdTot  = qtdProd-qtdCont;
         $(".value").val(qtdProdTot);
        $(this).data('done', true);
     }
});

$(".test").change(function(){ //if value was changed
     $(this).data('done', false);
});

小提琴: http : //jsfiddle.net/dNEmD/27/


Use the data in the dom element:

$(".test").focusout(function(){
     var qtdCont = parseInt($(this).val());
    if(qtdCont > 0 && $(this).data('done') == undefined)
    {
         var qtdProd     = $(".value").val();
         var qtdProdInt  = parseInt(qtdProd);
         var qtdProdTot  = qtdProd-qtdCont;
         $(".value").val(qtdProdTot);
        $(this).data('done', true);
     }
});

Fiddle: http://jsfiddle.net/dNEmD/19/

UPDATE

$(".test").focusout(function(){
     var qtdCont = parseInt($(this).val());
    if(qtdCont > 0 && 
         ($(this).data('done') == undefined || $(this).data('done') == false))
    {
         var qtdProd     = $(".value").val();
         var qtdProdInt  = parseInt(qtdProd);
         var qtdProdTot  = qtdProd-qtdCont;
         $(".value").val(qtdProdTot);
        $(this).data('done', true);
     }
});

$(".test").change(function(){ //if value was changed
     $(this).data('done', false);
});

Fiddle: http://jsfiddle.net/dNEmD/27/

相关问答

更多

从Struts2动作返回一个字符串到jQuery [复制](Return a string from Struts2 action to jQuery [duplicate])

您可以使用stream结果从操作中获取一个字符串。 将action设置为使用contentType设置为text/plain stream结果(或根本不使用contentType ,因为text/plain默认设置)。 <action name="callAction" method="call"> <result type="stream"> <param name="contentType">text/plain</param> </result> </acti...

jquery发布动作url(jquery getting post action url)

$('#signup').on("submit", function(event) { $form = $(this); //wrap this in jQuery alert('the action is: ' + $form.attr('action')); }); $('#signup').on("submit", function(event) { $form = $(this); //wrap this in jQuery alert('the ac...

如何不在jquery中复制一个动作(how do not duplicate a action in jquery)

使用dom元素中的data : $(".test").focusout(function(){ var qtdCont = parseInt($(this).val()); if(qtdCont > 0 && $(this).data('done') == undefined) { var qtdProd = $(".value").val(); var qtdProdInt = parseInt(qtdProd); ...

jquery提交不工作提交到action servlet中[复制](Jquery submit not working to submit into the action servlet [duplicate])

你需要有部署描述符(web.xml) <servlet> <servlet-name>servletName</servlet-name> <servlet-class>packageName.servletName</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletName</servlet-name> <url-pattern>/yourServletUrl</url-pattern> </se...

'action'是jQuery中的关键字吗?(Is 'action' a keyword in jQuery?)

动作是jQuery中的一个关键字,属性的值如何搞砸了一切? 不它不是。 你的问题出在其他地方。 也许您正试图直接在HTML头中获取表单,而不是等待页面完成加载。 你做了 <script> alert($('form').length); // May return 0 depending on the browser used. </script> ... <form> ... </form> 代替 <script> $(document).ready(function(...

点击iframe中的链接,然后执行jQuery动作[复制](Click on link in iframe then perform jQuery action [duplicate])

你可以 $('#iFrameID').contents().find('#linkID').click(function(){ $('.sliding_div_on_index_page').animate({'top': '-190px'}, 1000); }); ); 将链接 ID替换为您要触发包含该链接的iframe的ID的点击和iFrameID的iframe中链接的ID 。 例如,该代码可以在以下HTML中使用: 带有iframe的页面 : <iframe id = 'iFrameID...

JQuery选择器的多个操作(More than one action for a JQuery selector)

在jQuery中,你可以链接方法,因为它几乎总是返回一个jQuery对象。 所以,对于你现在的一段代码,你可以这样做: $('.myclass').css('color','#900').html('invalid'); 有些情况下,因为你需要额外的变量,所以你不能链接方法。 在这些情况下,你需要将jQuery对象缓存在一个变量中。 像这样的东西: var $el = $('.myclass'); $el.css('color','#900'); var t = 'invalid'; $el....

Action =“#”是什么意思?(What does Action=“#” mean? [duplicate])

Action="#"基本上是Post to self的html等价物。 #在技术上是一个锚,所以如果你点击一个只是"#"的链接就什么都不做。 浏览器应将任何Action="#"解释为Action="THE_HOSTNAME/PAGE" 它被设计为绕过W3C验证一次,因为Action=""被认为是无效的,因为所有属性都必须有值,这确保了属性可用的值。 Action="#" is basically the html equivalent of Post to self. # is technical...

相关文章

更多

Object Oriented Programming

Some might also contend that inheritance should be ...

Guava学习笔记:复写的Object常用方法

  在Java中Object类是所有类的父类,其中有几个需要override的方法比如equals,h ...

Solr: a custom Search RequestHandler

As you know, I've been playing with Solr lately, tr ...

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

Spring Data: a new perspective of data operations

Spring Data: a new perspective of data operations ...

自定义实现Hadoop Key-Value

自定义实现Value: 如果需要自定义一个一个Value类型,那么需要实现Hadoop预定义接口org ...

Python内建函数(A)

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整 ...

java 监听redis过期key事件实现延迟队列功能

java 监听redis过期key事件,可以实现简单的延迟队列功能。实现起来也特别简单,方法如下:一、 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本