How to remove a data from datatables pushed an array (maybe with pop) (javascript)

let array = [];
//you can try with a email that doens't exist into the array
let email = '[email protected]'
//
let recipientData = {
                        name: 'prueba1',
                        email: '[email protected]',
                    };
                    
array.push(recipientData)
                    recipientData = {
                        name: 'prueba2',
                        email: '[email protected]',
                    };
array.push(recipientData)
//im creating two new elemnts into the array
console.log(array)
//here we going to find the key of the element comparing the email
const index = array.findIndex(el => el.email === email);
    if (index === -1) {
    //if don't existe you can create a new one
    console.log('dont exist')
    }else{
    // and if exist we removed, array.splice(), index, and we specific than removed exactly one
    const removed = array.splice(index, 1)
    //and return the element removed
    console.log(removed)
    } 
    // and console.log array to verify
    console.log(array)

Hi, I leave you an example and the specifications of the code I leave it with comments in the code.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top