是否可以检查对象值的条件并返回密钥?(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/
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
从Struts2动作返回一个字符串到jQuery [复制](Return a string from Struts2 action to jQuery [duplicate])[2023-11-02]
您可以使用stream结果从操作中获取一个字符串。 将action设置为使用contentType设置为text/plain stream结果(或根本不使用contentType ,因为text/plain默认设置)。text/plain -
jquery发布动作url(jquery getting post action url)[2023-08-03]
$('#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 action ... -
使用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])[2022-09-26]
你需要有部署描述符(web.xml)servletName packageName.servletName servletName /yourServletUrl 动作是jQuery中的一个关键字,属性的值如何搞砸了一切? 不它不是。 你的问题出在其他地方。 也许您正试图直接在HTML头中获取表单,而不是等待页面完成加载。 你做了 ... 代替