0

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?

enter image description here

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; 
};

1 Answer 1

1

Use function _.every from lodash

https://lodash.com/docs/4.17.2#every


@Edit (based on comments) _.some(yourArray,{cart_item_supply: yourValue})

this will return you TRUE if in Array will be yourValue more about this you will find there: https://lodash.com/docs/4.17.4#some

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @DiPix I'm using now lodash. It's work. But I can't get the first id and get only this first id from others registers.
Can you give me an example? Collection, your lodash query, and expected result?
The beginning of the creation of the objects there are nothing. The user choose a product and create the first object arrays. So, he choice another product. If this new product has the same value of the "cart_item_supply" field of array, then acept and create a new object, otherwise show a message and don´t create a new object.
I think that I understand you. Please try this _.some(yourArray,{cart_item_supply: yourValue}) - this will return you TRUE if in Array will be yourValue more about this you will find there: lodash.com/docs/4.17.2#some - I hope it will help
@Ramos Mark as answered :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.