-1

i want to access the field names of an object nested within another object. In this example i want to map through user object. i tried to map through message ojbect and store it in a variable. then tried to map again but its giving an error saying Object is possibly 'null' or 'undefined'.

18 console.log(message[0].message.id);

 { message:
     { id: '0301939f-59b5-45f0-bc24-89a4d6840579',
       text: 'Everyone is here with the intention to improve.',
       html: '<p>Everyone is here with the intention to improve.</p>\n',
       type: 'regular',
       user: [Object],
       attachments: [Array],
       latest_reactions: [],
       own_reactions: [],
       reaction_counts: null,
       reaction_scores: {},
       reply_count: 0,
       cid: 'messaging:3',
       created_at: '2021-07-08T20:23:02.354861Z',
       updated_at: '2021-07-08T20:23:02.354861Z',
       shadowed: false,
       mentioned_users: [],
       silent: false,
       pinned: false,
       pinned_at: null,
       pinned_by: null,
       pin_expires: null,
       channel: [Object],
       title: 'Welcome to your pod d8jqzxzxp8@privaterelay.appleid.com!' } } ]
3

user object

{ id: 'masterHealthBot',
  role: 'admin',
  created_at: '2021-06-21T15:12:20.226915Z',
  updated_at: '2021-07-08T21:11:34.32916Z',
  banned: false,
  online: false,
  name: 'MasterHealth Bot' }
3


const message =  response.results.map(({ message }) => message.user);

console.log(message[0].id); // not working
console.log(message.map(({user})-> user.id)) give user undefined error

any help is greatly appreciated. thanks for your time.

Yuriy Yakym
  • 3,616
  • 17
  • 30
Leo Bogod
  • 369
  • 8
  • 28
  • What's your expected output? _"map through message object and store it in a variable"_ doesn't make much sense. Can't you simply assign/copy the object to a new variable? Also, you can't use `map` on an object unless you use something like `Object.keys`, `Object.values`, or `Object.entries` to get an array from the object. – Andy Jul 12 '21 at 12:07
  • Also `message` isn't an array so `message[0]` won't work. – Andy Jul 12 '21 at 12:13
  • i need to get d from user object – Leo Bogod Jul 12 '21 at 12:32
  • @LeoBogod from what I understand from the comments in the answers, messages is an array of objects, and user is an array objects. Don't you think that it might've been a good idea to 'share ' that with us ? Please learn how to ask questions properly. `i need to get d from user object` has nothing to do with what you are actually trying to do – Mihai T Jul 12 '21 at 13:15
  • @MihaiT you could have figured this out basing on this piece of code: `response.results.map` – Yuriy Yakym Jul 12 '21 at 13:16
  • @YuriyYakym Really ? how so ? Couldn't it be that the OP doesn't know the difference between object and array of objects ? and array methods ? Like another user ( Andy ) pointed out that `map` is an array method and it won't work on object `messages` . Also, it's not my job to 'figure out' what the OP wants and doesn't want. It's his job to be clear in his explanation – Mihai T Jul 12 '21 at 13:18
  • @MihaiT Yes, quite easily. If `response.results` was an object, OP would receive an error `.map is not a function` – Yuriy Yakym Jul 12 '21 at 13:25

2 Answers2

1

I don't really understand what exactly you are trying to do. But if user is nested inside message . Why don't you use message.user to get the User object? map is an array method the same goes for using [0] ( index selector ) .

EDIT: user is in fact an array of objects

So you could do something like this:

const message = {
  id: 'messageId1234',
  user: [{
    id: 'masterHealthBot',

  },
  {
    id: 'masterHealthBot2',

  }]
}
const users = message?.user;

users.map(user => console.log(user.id))
Mihai T
  • 17,254
  • 2
  • 23
  • 32
  • i just need to get all id value from user object – Leo Bogod Jul 12 '21 at 12:31
  • @LeoBogod and ? doesn't my answer provide that ? – Mihai T Jul 12 '21 at 12:34
  • well the answer just loops through entire object i only need id – Leo Bogod Jul 12 '21 at 12:34
  • how do i access the id field , user is an array of objects. in my case (message[0]) will get me only the first array within the object – Leo Bogod Jul 12 '21 at 12:36
  • Please be more clear. In your question you show the user as an object. Not as an array of objects. In my answer, the line `console.log(user.id)` returns just the ID . So, to be clear, the message object contains some key-value pairs and also an `user` key that is an array of objects ? eg `message : { id : 12121 , user: [{ id: user1}, { id: user2 }] }` ? – Mihai T Jul 12 '21 at 12:39
0

To get single user id, you can do messages[0].user[0].id.

If you want to get all the ids, consider using this code:

const messages = response.results;
const allUserIds = messages.flatMap((message) => {
  return message.user.map((user) => user.id);
});
console.log(allUserIds );

or using Array.prototype.reduce if Array.prototype.flatMap is unavailable:

const messages = response.results;
const allUserIds = messages.reduce((result, message) => {
  const messageUsersIds = message.user.map((user) => user.id);
  return [...result, ...messageUsersIds];
}, []);
console.log(allUserIds );
Yuriy Yakym
  • 3,616
  • 17
  • 30