简体   繁体   English

如果ID存在于使用angularjs的javascript数组中

[英]If ID exists in javascript array with angularjs

Okay, I'm done searching and trying code blocks that don't work. 好的,我已经完成搜索并尝试了无效的代码块。 This is what I got. 这就是我得到的。

I'm building an array when I click on an image. 当我单击图像时,我正在构建一个数组。

    invoices.addItem = function(id) {
        var itemRef = new Firebase(fbUrl+'/items/'+id);
        itemRef.once("value", function(snapshot) {
            invoices.newInvoiceItems.push({
                'id'    : id,
                'name'  : snapshot.val().name,
                'price' : snapshot.val().prices.sale,
                'qty'   : '1'
            });
        }); 
    }

I want to check to see if the ID passed through when the image is clicked again already exists in the array, if it does increase quantity. 我想检查数组中是否存在再次单击图像时通过的ID ,如果它确实增加了数量。

All I'm asking for is a simple way to check if the id exists, if it does return true else return false. 我要的只是一种简单的方法来检查id是否存在,如果返回true,否则返回false。

if(id exists)
{
  return true;
}
return false;

How do I get the id exists working!?!?!?!?!?!?!?! 我如何获取ID存在的有效信息!!!!!?!?!!!!!!!!

UPDATE 更新

This is everything I have: 这就是我拥有的一切:

invoices.addItem = function(id) {
        if(invoices.checkItem(id))
        {
            var index;
            for (index = 0; index < invoices.newInvoiceItems.length; ++index) {
                if(invoices.newInvoiceItems[index].id === id)
                {
                    var qty = invoices.newInvoiceItems[index].qty;
                    invoices.newInvoiceItems[index].qty = qty++;
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {

            var itemRef = new Firebase(fbUrl+'/items/'+id);
            itemRef.once("value", function(snapshot) {
                invoices.newInvoiceItems.push({
                    'id'    : id,
                    'name'  : snapshot.val().name,
                    'price' : snapshot.val().prices.sale,
                    'qty'   : '1'
                });
            });

        }
    }

    invoices.removeItem = function(index) {
        invoices.newInvoiceItems.splice(index, 1);
    }

    invoices.checkItem = function(id) {
        var index;
        for (index = 0; index < invoices.newInvoiceItems.length; ++index) {
            if(invoices.newInvoiceItems[index].id === id)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

Let's say I have two items Item A and Item B . 假设我有两个项目Item AItem B If I click on Item A it adds it to the array. 如果我单击项目A,它将添加到数组中。 Then click it again and it won't add it but only increases qty one time. 然后再次单击它,它不会添加,只会增加数量一次。 Now if I click on Item B without refreshing the page mind you, it adds it over and over and over again each time I click on Item B. 现在,如果我在不刷新页面的情况下单击项目B,则每次单击项目B时,它都会一遍又一遍地添加它。

I've console logged the id getting returned and it is always the same one, even though each item has it's own ID. 我已经在控制台记录了返回的ID,即使每个项目都有其自己的ID,它也始终是相同的。

You can use Array.prototype.filter to quickly and easily find an item in an array. 您可以使用Array.prototype.filter快速轻松地在数组中查找项目。 If the item is found... then you know it exists. 如果找到该项目...则说明它存在。

var existingItem = invoices.newInvoiceItems.filter(function(item){
    return item.id === id;
})[0];

if(existingItem ){
    ++existingItem.qty
}else{
    //do your firebase thing...
    var itemRef = new Firebase(fbUrl+'/items/'+id);
    itemRef.once("value", function(snapshot) {
        invoices.newInvoiceItems.push({
            'id'    : id,
            'name'  : snapshot.val().name,
            'price' : snapshot.val().prices.sale,
            'qty'   : 1
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM