如果在具有特定键的特定对象中,则检查值

loading...


0

我想检查 obj 中是否存在输入值,我显示警报('num found'),然后我在第二个 obj2 中按下该数字,并且该数字从 obj1 禁用,(我的意思是当我再次检查该数字时,它应该不可用),我不知道最好的方法是在对象中添加一些额外的键 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
            }

loading...

1回答

  • 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
     }
    
评论

暂时没有评论!