I cannot get a single array as an output to array.some
I have tried this code:
data = [{
Surname: 'Santos',
Firstname: 'Carlos'
},
{
Surname: 'Reyes',
Firstname: 'Carla'
},
{
Surname: 'Michael',
Firstname: 'Lebowski'
}
];
var found = data.some(function(data) {
return data.Surname === 'Reyes'
})
console.log(found);
The logs I get in return are:
0: {Surname: 'Santos', Firstname: 'Carlos'}
1: {Surname: 'Reyes', Firstname: 'Carla"}
2: {Surname: 'Michael', Firstname: 'Lebowski'}
My expected logs were:
0: {Surname: 'Reyes, Firstname:'Carla'}
How can I get my expected logs?
>Solution :
Use Array.Filter to get the method and pass your predicate.
data = [
{ Surname: 'Santos', Firstname: 'Carlos' },
{ Surname: 'Reyes', Firstname: 'Carla' },
{ Surname: 'Michael', Firstname: 'Lebowski' }
];
var foundArray = data.filter(function(item) {
return item.Surname === 'Reyes';
});
console.log(JSON.stringify(foundArray));