Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a specific array from an array of arrays?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading