Using the filter method an array of objects

0

I have an array of objects:

const musicData = [
    { artist: 'Adele', name: '25', sales: 1731000 },
    { artist: 'Drake', name: 'Views', sales: 1608000 },
    { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
    { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
    { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
    { artist: 'Original Broadway Cast Recording', 
      name: 'Hamilton: An American Musical', sales: 820000 },
    { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
    { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
    { artist: 'Rihanna', name: 'Anti', sales: 603000 },
    { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

I need to filter only names longer than 10 characters and less than 25. I made the following logic:

const results = musicData.filter((musicName) => {
  const {name} = musicName
  return name.length > 10 && name.length < 25
})

console.log(results);

However, the return is always the array with the correct objects, but not just the names. How can I make the result value to be just the array with the names instead of the entire object?

    
asked by anonymous 11.03.2018 / 02:51

2 answers

1

You can chain filter with map :

const musicData = [
  { artist: 'Adele', name: '25', sales: 1731000 },
  { artist: 'Drake', name: 'Views', sales: 1608000 },
  { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
  { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
  { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
  { artist: 'Original Broadway Cast Recording', 
    name: 'Hamilton: An American Musical', sales: 820000 },
  { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
  { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
  { artist: 'Rihanna', name: 'Anti', sales: 603000 },
  { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

const results = musicData.filter(musicName => {
  const { name } = musicName
  return name.length > 10 && name.length < 25
}).map(musicName => musicName.name)

console.log(results)

Edit: Same result with reduce , adapted from this answer:

const result = musicData.reduce((res, musicName) => {
  const { name } = musicName
  if (name.length > 10 && name.length < 25) {
    res.push(name);
  }

  return res;
}, []);
    
11.03.2018 / 03:08
0

Using only the filter

const musicData = [
  { artist: 'Adele', name: '25', sales: 1731000 },
  { artist: 'Drake', name: 'Views', sales: 1608000 },
  { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
  { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
  { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
  { artist: 'Original Broadway Cast Recording', 
    name: 'Hamilton: An American Musical', sales: 820000 },
  { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
  { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
  { artist: 'Rihanna', name: 'Anti', sales: 603000 },
  { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

// Utilizando apenas o método "filter"
const resultsFilter = [];
musicData.filter(music => (music.name.length > 10 && music.name.length < 25) && resultsFilter.push(music.name));
console.log('Utilizando "filter"', resultsFilter);

Reference

11.03.2018 / 23:09