1

I am trying to create record from a form data. When I console.log(req.body) I get the following record.

req.body => [Object: null prototype] {
  id: '1',
  key1: 'Value1',
  key2: 'Value2',
  supervisors: '[object Object],[object Object],[object Object]'
}

So I checked the database where the record is stored and see that supervisors is stored as:

supervisors: Array(3)
  0:
    name: "Reporter"
    userId: 4
  1:
    name: "Officer 1"
    userId: 5
  2:
    name: "Coordinator"
    userId: 2

I will like to get the values of userId as an array in supervisors field in the req.body so that my req.body will look like:

req.body => [Object: null prototype] {
  id: '1',
  key1: 'Value1',
  key2: 'Value2',
  supervisors: '[4, 5, 2]'
}
  1. I did const supervisors = JSON.stringify(req.body.supervisors)) and I got [object Object],[object Object],[object Object] when console logged

  2. I did const supervisors = q.supervisors ? JSON.parse(q.supervisors) : []; and I got SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse when console logged.

  3. I did const supervisors = req.body.supervisors.map(sup => sup.userId); and I got req.body.supervisors.map is not a function when console logged.

How can I get the supervisors value as [2, 4, 5]?

Wacademy
  • 101
  • 2
  • 13

2 Answers2

3

Use map()

const supervisors = q.supervisors.map(sup => sup.userId);

You don't need to use any JSON functions, as the data you show has already been parsed into an array of objects.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks @Barmar I used it as described and I got the error ```UnhandledPromiseRejectionWarning: TypeError: q.supervisors.map is not a function``` – Wacademy Jul 12 '21 at 19:21
  • I assumed `q` was the object you showed in the question. If not, replace it with the correct variable. – Barmar Jul 12 '21 at 19:25
  • in my case I assume q will be req.body so I replace it and still got the error ```req.body.supervisors.map is not a function``` – Wacademy Jul 13 '21 at 03:39
  • please take a look again at the question I have updated it with more details – Wacademy Jul 13 '21 at 04:21
  • The client didn't send the parameters correctly. They concatenated the supervisor objects, which converted them to strings `[object Object]`. – Barmar Jul 13 '21 at 05:01
  • See https://stackoverflow.com/questions/4750225/what-does-object-object-mean – Barmar Jul 13 '21 at 05:02
0

As mentioned above, map() is the correct one-line approach.

map() using an arrow function (which has an implicit return) is also the ideal approach if you need to chain further transformations.

The alternative (verbose but also lightning-fast) approach is (that old work-horse) the for loop.


Working Example:

// THE OBJECT
const myObject = {

  supervisors: [

    {
      name: 'Reporter',
      userId: 4
    },

    {
      name: 'Officer 1',
      userId: 5
    },

    {
      name: 'Coordinator',
      userId: 2
    }
  ]
};

// THE SUPERVISORS ARRAY (NOT YET POPULATED)
const supervisors = [];

// LOOP THROUGH THE ARRAY IN THE OBJECT TO POPULATE THE SUPERVISORS ARRAY
for (let i = 0; i < myObject.supervisors.length; i++) {

  supervisors.push(myObject.supervisors[i].userId);
  
  console.log(supervisors);
}
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • Thanks @Rounin when I do I got the error ```UnhandledPromiseRejectionWarning: ReferenceError: myObject is not defined``` – Wacademy Jul 12 '21 at 19:33
  • `myObject` is an _example name_ (like `foo` and `bar`) - in the example above, it's a stand-in for whatever variable name you're actually using. – Rounin Jul 13 '21 at 00:41
  • When I do, I got a long undefined value. Meanwhile I updated the question with more details supervisors => [ undefined ] supervisors => [ undefined, undefined ] supervisors => [ undefined, undefined, undefined ] – Wacademy Jul 13 '21 at 02:22
  • please take a look again at the question I have updated it with more details – Wacademy Jul 13 '21 at 04:21
  • @Wacademy - you've been given two clear answers on this page showing you how to build a simple array from an object: 1) using the `map()` function; 2) using a `for loop` . You surely have enough to write your own working code at this point. Are there any parts of either of those two answers which are not making sense to you? – Rounin Jul 13 '21 at 09:29