The modified array with the element toggled (added or removed).
const pets = [
{ type: 'dog', name: 'Fido' },
{ type: 'dog', name: 'Chacalin' },
{ type: 'cat', name: 'Mr. Brown' },
{ type: 'cat', name: 'Night' },
]
PUtilsArray.toggleElement(pets, { type: 'dog', name: 'Chacalin' }, pet => pet.name == 'Chacalin')
console.log(pets)
// [
// { type: 'dog', name: 'Fido' },
// { type: 'cat', name: 'Mr. Brown' },
// { type: 'cat', name: 'Night' },
// ]
PUtilsArray.toggleElement(pets, { type: 'dog', name: 'Chacalin' }, pet => pet.name == 'Chacalin')
console.log(pets)
// [
// { type: 'dog', name: 'Fido' },
// { type: 'cat', name: 'Mr. Brown' },
// { type: 'cat', name: 'Night' },
// { type: 'dog', name: 'Chacalin' },
// ]
Inserts an element into an array if the
check
callback function returnsfalse
for every element. Otherwise, it removes the element(s) for wichcheck
returnstrue
.