1

Is there a way to write this in one line?

  var data = {};
  data["limit"] = [1, 0]; 
  data["where"] = [0]; 
  data["order"] = [];
  data["display"] = [0];

Something like

var data = {["limit"][1,0],["where"][0]...}
user3565264
  • 157
  • 2
  • 10
  • http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – smali May 15 '15 at 04:13
  • 1
    [Instantiating a javascript object and populating its properties in a single line](http://stackoverflow.com/questions/11777357/instantiating-a-javascript-object-and-populating-its-properties-in-a-single-line) – Downgoat May 15 '15 at 04:14

3 Answers3

8

Javascript objects can be written in a one-liner like this:

var data = {limit: [1, 0], where: [0], order: [], display: [0]};
royhowie
  • 11,075
  • 14
  • 50
  • 67
Val
  • 207,596
  • 13
  • 358
  • 360
5

You can use an object initializer (the quotes around property names are in fact optional):

var data = {
  'limit': [1,0],
  'where': [0],
  'order': [],
  'display': [0]
};
lc.
  • 113,939
  • 20
  • 158
  • 187
  • 1
    @vihan1086: Everything in Javascript can be made one line by just stripping away the line breaks… – Bergi May 15 '15 at 04:16
  • @vihan1086 I interpret "one line" (and "one-liner") to mean one *statement*, not one physical line. Otherwise for any language which does not use a newline as a delimiter (C family, Java, JavaScript, etc), the entire program can technically be written as one line. As can the original code in the question, frankly! – lc. May 15 '15 at 04:45
0

My json skills are minimal, but it should be

{"limit":[1,0], "where":[0], "order":[], "display":[0]} 

or some permutation of that (order in an object isn't meaningful as far as I know).

keshlam
  • 7,931
  • 2
  • 19
  • 33