是否可以检查对象值的条件并返回密钥?(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
满意答案
使用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])
jquery发布动作url(jquery getting post action url)
如何不在jquery中复制一个动作(how do not duplicate a action in jquery)
jquery提交不工作提交到action servlet中[复制](Jquery submit not working to submit into the action servlet [duplicate])
'action'是jQuery中的关键字吗?(Is 'action' a keyword in jQuery?)
点击iframe中的链接,然后执行jQuery动作[复制](Click on link in iframe then perform jQuery action [duplicate])
JQuery选择器的多个操作(More than one action for a JQuery selector)
Action =“#”是什么意思?(What does Action=“#” mean? [duplicate])
相关文章
更多Object Oriented Programming
Guava学习笔记:复写的Object常用方法
Solr: a custom Search RequestHandler
Becoming a data scientist
Spring Data: a new perspective of data operations
自定义实现Hadoop Key-Value
Python内建函数(A)
java 监听redis过期key事件实现延迟队列功能
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!