JS: Filter Array of Objects

Let’s say we have this …

const runCmd = [
  { scope: 'js', cmd: 'babel-node' },
  { scope: 'ruby', cmd: 'ruby -W0' },
  { scope: 'php', cmd: 'php -f' },
  { scope: 'python', cmd: 'python' },
  { scope: 'shell', cmd: 'bash' }

And we want to get the cmd for each language scope, except for Javascript. (Say, we have another check for js but not jsx.) We could either do this.

  .filter(function(rc) {
    return rc.scope !== 'js'
  .forEach(function(rc) {

Or we can do this.

runCmd.filter(rc => rc.scope !== 'js').forEach(rc => console.log(rc.cmd))

Actual use of the script …



(This is the reason why I like Atom more than VSCode. It is so easy whipping up a quick script for some simple thing. I don’t need to write a whole plugin.)