-3

Was trying to send some static data to websocket and receive the same. The data is

var details=
    [
      {
        "_id": "5799fac61ee480492224071a",
        "index": 0,
        "age": 25,
        "name": "Jean Pierce",
        "gender": "female",
        "email": "jeanpierce@cincyr.com"
      },
      {
        "_id": "5799fac678aae9a71af63ef2",
        "index": 1,
        "age": 23,
        "name": "Laurie Lopez",
        "gender": "female",
        "email": "laurielopez@cincyr.com"
      },
      {
        "_id": "5799fac6c237d929693c08d7",
        "index": 2,
        "age": 21,
        "name": "Padilla Barrett",
        "gender": "male",
        "email": "padillabarrett@cincyr.com"
      }
    ];

When the above data is received, it is in the form of string, so I parsed it to JSON using JSON.parse(data) and store it in the local variable. Now, the issue here is, for parsing, it need to be converted to [Object,Object] form instead of [object Object],[object Object] which is coming on the console. How to achieve this ?

Update: The code for the same is as below, please have a check.

Sending data to websocket server using the below code

 if (client.readyState === client.OPEN) {               
                    client.send(JSON.stringify(details));                   
                }

On the receiving end the data is received as -

client.onmessage = function(e) {
         console.log(" Fetched data :"+JSON.parse(e.data));
          this.setState({   val:   JSON.parse(e.data)
                    });
            }.bind(this);

the console.log() in the second statement is showing the result as [object Object],[object Object] but for parsing the same in react [ Object Object] is needed. How to achieve this ?

Console image

Worker
  • 283
  • 1
  • 9
  • 20
  • 1
    "I parsed it to JSON" — **from** not to. – Quentin Jul 28 '16 at 12:48
  • Are you sure your console isn't just displaying an array of objects? – gcampbell Jul 28 '16 at 12:48
  • I have no idea what you are asking. You appear to be stringifying (although you've failed to provide a [MCVE]) an array of objects, so `"[object Object],[object Object]"`would be the expected, but not very useful result. I don't see how `"[object Object]"` would be any more useful though. – Quentin Jul 28 '16 at 12:49
  • @gcampbell yes, I have written the same what is coming in the console. – Worker Jul 28 '16 at 12:49
  • @gcampbell have now updated the question, have a check on what I am exactly trying to achieve. – Worker Jul 28 '16 at 12:59
  • It's showing as `[object Object],[object Object]` because the JSON is an array of objects, so each one's `toString` returns `[object Object]`, and the array is being joined with a comma. – gcampbell Jul 28 '16 at 13:01
  • @Worker — It doesn't help at all. You are getting that string logged because you are converting the array to a string **when you log it** (but not when you do anything else with it). If you don't want an **array** of things each with an id, name, etc but you just want a **single** thing then which single thing do you want? – Quentin Jul 28 '16 at 13:02
  • @Quetin, I am getting array as a string, so had to convert it to JSON. I want array of things in a single array and not group of arrays. – Worker Jul 28 '16 at 13:08
  • @Worker — You have a single array. If you think you don't, then you are misinterpreting what `[{}, {}].toString()` does (which is what you are essentially doing when you get down to it). – Quentin Jul 28 '16 at 13:09

1 Answers1

-1

Here you go:

console.log('[' + array.fill('Object').toString() + ']')

Test:

var array = [
  {
    "_id": "5799fac61ee480492224071a",
    "index": 0,
    "age": 25,
    "name": "Jean Pierce",
    "gender": "female",
    "email": "jeanpierce@cincyr.com"
  },
  {
    "_id": "5799fac678aae9a71af63ef2",
    "index": 1,
    "age": 23,
    "name": "Laurie Lopez",
    "gender": "female",
    "email": "laurielopez@cincyr.com"
  },
  {
    "_id": "5799fac6c237d929693c08d7",
    "index": 2,
    "age": 21,
    "name": "Padilla Barrett",
    "gender": "male",
    "email": "padillabarrett@cincyr.com"
  }
]

console.log('[' + array.fill('Object').toString() + ']')

Though I don't honestly know why you'd want this over the other - both are equally (un)helpful. Are you sure you don't want console.dir?

Community
  • 1
  • 1
Nebula
  • 6,614
  • 4
  • 20
  • 40
  • Have updated the question. Please check why I was trying to achieve the same. And, it's not used for console, instead for assigning value. – Worker Jul 28 '16 at 13:04
  • Honestly your question still isn't very clear. I *am* doing what you asked for: "it needs to be converted to `[Object,Object]` form instead of `[object Object],[object Object]`". Instead, let's imagine my answer as a function. Please explain what the output should be for a given input, as an example, so I can make a better answer. – Nebula Jul 28 '16 at 16:15