我想检查输入值是否存在于obj
中,我显示一个警报(“找到数量”),然后在第二个obj2
中推送该数量,并且该数量被禁用来自obj1
,(我是说当我再次检查num时,它应该不可用),我不知道最好的方法是在对象中添加一些额外的密钥used
还是创建一个新对象obj2
,然后首先检查是否已使用这些值。
var obj = [
{"id":1, "num":123, "val":20, "type":"weekend"},
{"id":2, "num":456, "val":30, "type":"weekend"},
{"id":3, "num":789, "val":15, "type":"semaine"},
{"id":4, "num":101, "val":25, "type":"semaine"}
];
var obj2 = [
];
html:
<input id="code"/> // val = 15
Js
jQuery("input").on("keydown",function search(e) {
if(e.keyCode == 13) { //when hit enter
var tik = jQuery(this).val();
console.log(tik);
var obj = [
{"id":1, "num":123, "val":20, "type":"week"},
{"id":2, "num":456, "val":30, "type":"week"},
{"id":3, "num":789, "val":15, "type":"day"},
{"id":4, "num":101, "val":25, "type":"day"}
];
if (tik in obj2.num) { // It doesn't work for me
alert("num found");
//I push this value to obj2
...
} else {
alert("num not found");
//I don't push to obj2
}
答案 0 :(得分:0)
您的情况可以是这样的:
if (obj.find(o => o.num === parseInt(tik))) {
alert("num found");
//I push this value to obj2
} else {
alert("num not found");
//I don't push to obj2
}