Kelly Nelson
Kelly Nelson

Reputation: 47

Filter Javascript array and return only objects that have a populated key

I've been searching here for a few hours and can't quite find the right answer so far. My apologies if the answer is already out there.

I'm trying to filter an array and return only the objects that have a certain key with any value. Doesn't matter was the value is, just as long as that key is not empty or null.

Array Example:

[
  {
    "title": "Song 1",
    "author": "Joe",
    "Album1": 2,
    "Album2": null,
    "Album3": 5
  },
  {
    "title": "Song 2",
    "author": "Jane",
    "Album1": null,
    "Album2": null,
    "Album3": 8
  },
  {
    "title": "Song 3",
    "author": "Steve",
    "Album1": 10,
    "Album2": 1,
    "Album3": null
  }
]

I'd like to only return the objects that have a value in Album1 (that value would be the track #). I don't care what the value is, just as long as there's a value. So in this example, only return the following to a new array:

[
  {
    "title": "Song 1",
    "author": "Joe",
    "Album1": 2,
    "Album2": null,
    "Album3": 5
  },
  {
    "title": "Song 3",
    "author": "Steve",
    "Album1": 10,
    "Album2": null,
    "Album3": null
  }
]

I've tried several different methods, each failing. Here's the one that seems like it should work, but doesn't:

getFilteredSongs (array, album) {
  return songs
    .filter(function (obj) {
      return Object.keys(album).every(function (a) {
        return obj[a] === true
      })
    })
  }

getFilteredSongs(songsList, Album1)

I greatly appreciate any assistance as well as patience with my green skills and inability to find what seems like a question that should be out there.

Upvotes: 1

Views: 41

Answers (3)

Scott Rudiger
Scott Rudiger

Reputation: 1300

In case it's possible that Album1 could be a falsy value other than undefined/null (such as 0), you could exclusively check for only undefined/null as follows:

const songs = [
  {
    "title": "Song 1",
    "author": "Joe",
    "Album1": 2,
    "Album2": null,
    "Album3": 5
  },
  {
    "title": "Song 2",
    "author": "Jane",
    "Album1": null,
    "Album2": null,
    "Album3": 8
  },
  {
    "title": "Song 3",
    "author": "Steve",
    "Album1": 10,
    "Album2": 1,
    "Album3": null
  }
];

const filteredSongs = songs.filter(({Album1}) => Album1 != null); // filters out only undefined/null
console.log(filteredSongs);

Upvotes: 0

SteveB
SteveB

Reputation: 924

The following will eliminate any album that has null.

filteredSongs = songs.filter((cur) => cur.Album1 !== null);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370659

All you need to do is return the Album1 property - if null, the resulting array won't be included in the result:

const songs = [
  {
    "title": "Song 1",
    "author": "Joe",
    "Album1": 2,
    "Album2": null,
    "Album3": 5
  },
  {
    "title": "Song 2",
    "author": "Jane",
    "Album1": null,
    "Album2": null,
    "Album3": 8
  },
  {
    "title": "Song 3",
    "author": "Steve",
    "Album1": 10,
    "Album2": 1,
    "Album3": null
  }
];

const filtered = songs.filter(({ Album1 }) => Album1);
console.log(filtered);

Or, without destructuring, if you prefer:

const filtered = songs.filter(song => song.Album1);

Or, if you call getFilteredSongs with a string key of Album1:

const songs = [
  {
    "title": "Song 1",
    "author": "Joe",
    "Album1": 2,
    "Album2": null,
    "Album3": 5
  },
  {
    "title": "Song 2",
    "author": "Jane",
    "Album1": null,
    "Album2": null,
    "Album3": 8
  },
  {
    "title": "Song 3",
    "author": "Steve",
    "Album1": 10,
    "Album2": 1,
    "Album3": null
  }
];
const getFilteredSongs = (songs, album) => songs.filter(song => song[album]);
console.log(getFilteredSongs(songs, 'Album1'));

Upvotes: 2

Related Questions