I have a question in Ionic/AngularJs:
I have two searches on an object that are not working properly.
The first if (cartObj.cart.find (id)! = - 1)
{ ---> This is okay.
The second is an if (cartObj.cart.findvovo (supply_id)! = - 1) { were if the value of the first if is equal to the field I want to fetch it accepts.
I want to fetch data only from "cart_item_supply": supply_id. How can I do this?
In the screen attached I show my console with the array objects for you understand better.
This is my code:
var cartObj = {};
cartObj.cart=[];
cartObj.total_amount=0;
cartObj.total_qty=0;
// VERIFICA DE J�? EXISTE ITENS NO CARRINHO
cartObj.cart.add=function(id,image,name,price,qty,supply_id,deliver){
if( cartObj.cart.find(id)!=-1 ){
var alertPopup = $ionicPopup.alert({
title: 'This product has already been added',
template: 'Add more quantity to cart'
});
}if( cartObj.cart.findvovo(supply_id) !=-1){
var alertPopup = $ionicPopup.alert({
title: 'This offer is from Another Grandmother',
template: 'You can only buy offers from a single Grandma. Choose other offers from this same Grandma.'
});
}
// IF DON´T EXIST, ADD
else{
cartObj.cart.push( { "cart_item_id": id , "cart_item_image": image , "cart_item_name": name , "cart_item_price": price , "cart_item_qty": qty, "cart_item_supply": supply_id, "cart_item_deliver": deliver } );
cartObj.total_qty+=1;
cartObj.total_amount+=parseInt(price);
console.log(cartObj);
}
};
// SEACH PRODUCTS BY ID
cartObj.cart.find=function(id){
console.log("chamou find");
var result=-1;
for( var i = 0, len = cartObj.cart.length; i < len; i++ ) {
if( cartObj.cart[i].cart_item_id === id ) {
result = i;
console.log(result);
break;
}
}
return result;
};
// SEARCH CART_ITEM_SUPPLY === SUPPLY_ID --- HERE DONT FOUND
cartObj.cart.findvovo=function(supply_id){
var result=-1;
for( var i = 0, len = cartObj.cart.length; i < len; i++ ) {
if( cartObj.cart[i].cart_item_supply === supply_id ) {
result = i;
console.log(result);
break;
}
};
return result;
};
Can you do resolve this question?
The especific code is here:
// SEARCH CART_ITEM_SUPPLY === SUPPLY_ID --- HERE DONT FOUND
cartObj.cart.findvovo=function(supply_id){
var result=-1;
for( var i = 0, len = cartObj.cart.length; i < len; i++ ) {
if( cartObj.cart[i].cart_item_supply === supply_id ) {
result = i;
console.log(result);
break;
}
};
return result;
};