Using the filter method an object array

Asked

Viewed 379 times

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 with more 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 value of result be just the array with the names and not the whole object?

2 answers

1


You can chain the filter with a 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: the same result with reduce, adapted of that reply:

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

  return res;
}, []);
  • But can I do without chaining? Using only filter?

  • With only one function, I don’t think I can filter, with only reduce.

0

Using only the method 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

  • But the question was to use the filter. Then it wouldn’t happen to use the foreach.

  • Note that I put 3 examples, the last one is just using filter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.