5

I have below javascript method. I try to get the id from the data parameter.

data(spark, data){
    console.log('receive ', data)
    console.log(data.id)
  }

the first output line is receive {id:1}

but the second output line is undefined

then I tried below method to convert the json string to object:

data(spark, data){
    console.log('receive ', data)
    console.log(JSON.parse(JSON.stringify(data)).id)

I still got the same output. Why can't I get the id from the input parameter?

EDIT1

I changed the parameter name to be different with the function name as below:

data(spark, d){
    console.log('receive ', d)
    console.log(JSON.parse(JSON.stringify(d)).id)
}

but I still got the same output.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

3 Answers3

2

Note: Strong word of caution. Just checking if this works, and it works. Do not use it in unexpected circumstances. Very dangerous!

One crazy thing is, you forgot the function keyword, in-front of the function name.

Trying with eval() for this:

function data(spark, d) {
  // console.log('receive ', d);
  eval("d = " + d);
  console.log(d.id);
}
data("", "{id: 5}");
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Four answers:

  1. Have the server send down properly formed JSON (with double quotes around the key names). This is the preferred solution.

  2. Use a relaxed JSON parser. You can search and find such things on npm.

  3. Use eval.

  4. Do some string manipulation to enclose the id in double quotes so you can JSON.parse it, or extract the 1, or whatever you want to do. This is the least preferred solution.

  • **1.** What if the server is not under control? **2.** NPM? Seriously? – Praveen Kumar Purushothaman Dec 20 '16 at 05:49
  • I'm not recommending using a relaxed/dirty parser, but I think it's better than eval. What is your problem with npm? Where else would you go looking for one? –  Dec 20 '16 at 05:52
  • I thought NPM is only for Node? Am I wrong? – Praveen Kumar Purushothaman Dec 20 '16 at 05:52
  • Easy enough to use node packages in the browser with something like browserify or webpack. –  Dec 20 '16 at 05:53
  • And the crazy part is, you asked me not to use and you have added `eval()` as well. – Praveen Kumar Purushothaman Dec 20 '16 at 05:53
  • Not agreeing with the Webpack thing. What if it is a shared hosting? – Praveen Kumar Purushothaman Dec 20 '16 at 05:54
  • No, I didn't say not to use `eval`. I just said there's a better ("correct") solution. –  Dec 20 '16 at 05:54
  • Okay... I sincerely feel that you are missing a main major point. Let's assume the worst case. OP is using a shared hosting plus the server side is not under control. What would you recommend? – Praveen Kumar Purushothaman Dec 20 '16 at 05:55
  • @PraveenKumar can't parse your comment about webpack. It the OP has a JS program running in a browser, then he can arrange with a bit of pre-processing to bundle a node module into his JS. This has nothing to do with where his site is hosted. –  Dec 20 '16 at 05:55
  • I kinda feel it would be slightly dizzy for the OP. I come from JS background and started working with WebPack and other things. It feels quite annoyed to set up stuff, but since I am using a shared hosting, none of the Node stuff I can think of. Pure HTML, CSS, JS in the client side. Just my few cents of disappointment. – Praveen Kumar Purushothaman Dec 20 '16 at 05:57
-2

eval("(" + data + ")").id is all that is required

function data(spark, data) {
  alert(eval("(" + data + ")").id);
}

data("", "{id:3}");
mildog8
  • 2,030
  • 2
  • 22
  • 36