-1

How to know the user's name with javascript

[
  {
    id: 3843558,
    cid: 4070139,
    text: ' Hi today',
    user: 'Eklavya',
    rto: [ [Object], [Object], [Object], [Object] ],
    time: 1616050866
  }
]
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
Eklavya Chandra
  • 108
  • 1
  • 11

2 Answers2

1

If the variable is called users then in this case users[0].user or users[0]['user']. users = JSON.parse(str) if you have string str. fs = require('fs'); fs.readFile(file, [encoding], [callback]); if you need to read the string from file (nodejs).

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
1

const arr = [
   {
     id: 3843558,
     cid: 4070139,
     text: ' Hi today',
     user: 'Eklavya',
     rto: [ [Object], [Object], [Object], [Object] ],
     time: 1616050866
   }
 ]
    //If multiple object exist..
    const users = arr.map(obj=>{
      return obj.user
    })

    //For single object
    let name = arr[0].user
   
    //OR
    let name = (arr[0])['user']