0

I am working with handsontable and a jsfiddle http://jsfiddle.net/kc11/cb920ear/ . In this there is the following js function:

  function getCarData() {
    return [
      {car: "Mercedes A 160", year: 2006, available: true, comesInBlack: 'yes'},
      {car: "Citroen C4 Coupe", year: 2008, available: false, comesInBlack: 'yes'},
      {car: "Audi A4 Avant", year: 2011, available: true, comesInBlack: 'no'},
      {car: "Opel Astra", year: 2004, available: false, comesInBlack: 'yes'},
      {car: "BMW 320i Coupe", year: 2011, available: false, comesInBlack: 'no'}
    ];
  }

What is this data structure is this? I can see its not a 2d array or json. Is there an easy way to change it to json which I will need to receive from a server to load the handsontable?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 3
    Looks exactly like a 2D array to me, containing objects ? – adeneo Dec 31 '14 at 20:28
  • 2
    Other than `JSON.stringify()`? – Ignacio Vazquez-Abrams Dec 31 '14 at 20:29
  • What *exactly* do you need to do with this array? What does `handsontable` expect? I don't see any need to convert this to a JSON string. You just to convert it to a format that `handsontable` can use, which I assume is an array or an object, not a JSON string. – gen_Eric Dec 31 '14 at 20:37
  • Sorry, I went back and looked again, and I realize that I misread http://stackoverflow.com/questions/4329092/multi-dimensional-associative-arrays-in-javascript and it is indeed a 2D array or array of objects – user1592380 Dec 31 '14 at 20:37
  • Rocket, I want to change the example data in getCarData() to json so that I can simulate a db backend that produces json for consumption by handsontable. – user1592380 Dec 31 '14 at 20:39
  • You want to "simulate a db backend" with JavaScript? I'm a little confused here. – gen_Eric Dec 31 '14 at 20:40
  • I'm sorry if I'm not clear. I have a php backend that produces a json representation of a db table. I want to use a frontend view with handsontable to display this. – user1592380 Dec 31 '14 at 20:52
  • try `JSON.stringify(getCarData())` then push the result to your backend – Harry Moreno Dec 31 '14 at 21:45
  • Your fiddle has an error when you click the button: Cannot read property 'getData' of undefined – Mark Schultheiss Dec 31 '14 at 22:04
  • It returns an object with an array. Perhaps a simple example: alert(getCarData()[2].car); which alerts "Audi A4 Avant" as in this fiddle will help you understand? http://jsfiddle.net/h6nh1gvw/ – Mark Schultheiss Dec 31 '14 at 22:21
  • Mark, thanks for the fiddle, that helps. What does "object with an array" mean? array of objects? – user1592380 Jan 01 '15 at 01:29

1 Answers1

0

To turn a function into json do

JSON.stringify(getCarData, function(key, value) {
  if (typeof value === 'function') {
    return value.toString();
  }
  return value;
});

returned ""function getCarData() {\n return [\n {car: \"Mercedes A 160\", year: 2006, available: true, comesInBlack: 'yes'},\n {car: \"Citroen C4 Coupe\", year: 2008, available: false, comesInBlack: 'yes'},\n {car: \"Audi A4 Avant\", year: 2011, available: true, comesInBlack: 'no'},\n {car: \"Opel Astra\", year: 2004, available: false, comesInBlack: 'yes'},\n {car: \"BMW 320i Coupe\", year: 2011, available: false, comesInBlack: 'no'}\n ];\n }"" to me. See http://www.kristofdegrave.be/2012/07/json-serialize-and-deserialize.html

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
  • Shouldn't that just be `JSON.stringify(getCarData())`? Why are you trying to convert the *function* itself into JSON? He wants to manipulate the *return value* of that function. – gen_Eric Dec 31 '14 at 20:37
  • Also, it's *not* an array of "json objects". It's an array of [JavaScript] objects. JSON is a *string representation* of data; if it's not a string, it's not JSON. – gen_Eric Dec 31 '14 at 21:20
  • err the question title is 'How to turn javascript function to json' so I showed how to turn his function into a json string. – Harry Moreno Dec 31 '14 at 21:36
  • Also `JSON.stringify()` will return undefined. Hence the need for the callback provided above. – Harry Moreno Dec 31 '14 at 21:39
  • Ah I see you passed the result of executing the function into JSON.stringify. This would stringify the result but not serialize the function. Depends on what you want really. – Harry Moreno Dec 31 '14 at 21:44
  • Pretty sure he want's the result of the function call, not the function itself. P.S. Not many people here seem to know how to write good question titles. – gen_Eric Dec 31 '14 at 21:48
  • Rocket is correct in what I meant, Harry but I'm accepting your answer because you answered the question I actually asked. – user1592380 Jan 01 '15 at 02:16