I am using reactjs have the following data below and I want to loop through the array and remove the two entry.
0: {mailId: '[email protected]', firstName: 'one'}
1: {mailId: '[email protected]', firstName: 'two'}
2: {mailId: '[email protected]', firstName: 'three'}
3: {mailId: '[email protected]', firstName: 'four'}
4: {mailId: '[email protected]', firstName: 'five'}
The below two email I want to check it against array and needs to remove it.
[email protected]
[email protected]
I'm expecting a final array like below
0: {mailId: '[email protected]', firstName: 'one'}
1: {mailId: '[email protected]', firstName: 'two'}
2: {mailId: '[email protected]', firstName: 'four'}
If the mail id is only one we can remove like below, but if mailId is again list/array then how can we remove it, please help me.
arrayfilter.filter((item) => item.mailId !== "[email protected]")
CodePudding user response:
Use filter and check if your blacklist emails are includes the mailId
const arr = [{mailId: '[email protected]', firstName: 'one'},
{mailId: '[email protected]', firstName: 'two'},
{mailId: '[email protected]', firstName: 'three'},
{mailId: '[email protected]', firstName: 'four'},
{mailId: '[email protected]', firstName: 'five'},]
const blackList = ["[email protected]", "[email protected]"]
const result = arr.filter(item => !blackList.includes(item.mailId))
console.log(result)
CodePudding user response:
const data = [
{mailId: '[email protected]', firstName: 'one'},
{mailId: '[email protected]', firstName: 'two'},
{mailId: '[email protected]', firstName: 'three'},
{mailId: '[email protected]', firstName: 'four'},
{mailId: '[email protected]', firstName: 'five'},
]
let newArray = [];
const remove = ['[email protected]', '[email protected]']
data.forEach(ele => {
if(!remove.includes(ele.mailId)) {
newArray.push(ele)
}
})
console.log(newArray)
CodePudding user response:
arrayfilter.filter((item) => !["[email protected]", "[email protected]"].includes(item.mailId))