0

I have this code here.

const find = require('find-process');

find('name', 'TIDAL.exe')
  .then(function (list) {
    console.log(list)
  }, function (err) {
    console.log(err.stack || err);
});

which returns this here

[
  {
    pid: 7752,
    ppid: 9280,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 500,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 1100,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 6424,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 13692,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 3160,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  }
]

I need to extract the "pid" values and store them in an array but I'm stuck on how I should do it. I've looked around and tried some things out but I just can't get it to work.

Sudhanshu Kumar
  • 1,926
  • 1
  • 9
  • 21
dethm0r
  • 23
  • 3

2 Answers2

2

let data=[{pid:9280,bin:"...",name:"TIDAL.exe",cmd:"..."},{pid:500,ppid:7752,bin:"...",name:"TIDAL.exe",cmd:"..."},{pid:1100,ppid:7752,bin:"...",name:"TIDAL.exe",cmd:"..."},{pid:6424,ppid:7752,bin:"...",name:"TIDAL.exe",cmd:"..."},{pid:13692,ppid:7752,bin:"...",name:"TIDAL.exe",cmd:"..."},{pid:3160,ppid:7752,bin:"...",name:"TIDAL.exe",cmd:"..."}];

let result = data.map(e => e.pid)

console.log(result)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
0

You can use map function for getting the array of PID's

var data = [{
    pid: 9280,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 500,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 1100,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 6424,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 13692,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  },
  {
    pid: 3160,
    ppid: 7752,
    bin: '...',
    name: 'TIDAL.exe',
    cmd: '...'
  }
];

let pids = data.map(item => item.pid);
console.log(pids);

It basically returns a new array with the filter that you've applied, Here I return pid of each item and map them into an array.

So your code should be

    const find = require('find-process');

    find('name', 'TIDAL.exe')
      .then(function (list) {
        let pids = list.map(item => item.pid);
        console.log(pids);
      }, function (err) {
        console.log(err.stack || err);
    });

Learn more on Array.prototype.map() here

Sudhanshu Kumar
  • 1,926
  • 1
  • 9
  • 21