1

I want to create an array of JSON object without a key.How can this is achieved ..??

for example [{8,0,2}, {20,0,2}].

var hh = 9
var mm = 8
var qty = 2
var data = [];
data.push({hh,mm,qty})

it gives data like [{hh:9,mm:8,qty:2}] I want array like [{9,8,2},{9,3,4}]

Ved Prakash
  • 29
  • 1
  • 1
  • 4

5 Answers5

1

Without key value pair object was not created .so that's why its adding key name from variable name

see this you error.Its a invalid one

var a = [{8,0,2}, {20,0,2}];
console.log(a)

You could push array instead of object

var data = [];
var hh = 9
var mm = 8
var qty = 2
var data = [];
data.push([hh,mm,qty])

console.log(data)
prasanth
  • 19,775
  • 3
  • 25
  • 48
0

You can't.

Object Literal Property Value Shorthands allow you to create an object where the property names are inferred from the variable names you use to pass the data into.

If you don't have variable names, then there is nothing for the JS engine to use to figure out what the property names should be.

Consider using a function instead.

console.log([time(8,0,2), time(20,0,2)]);

function time (hh, mm, qty) {
    return {hh, mm, qty};
}
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

The result you get at the end makes sense. By doing {hh,mm,qty} you are effectively saying "Use the variable name as the key and its value as the value". It might help us more if you provide an example of how you intend to use the result and access the variables i.e. the shape of the object you want in the end.

That being said, there are a couple alternatives:

Using values as the keys

If you really wanted your object to look similar to {8,0,2} you could use those values as the keys (all keys get converted to strings anyways) so you could do the following:

var example1 = {8:undefined,0:undefined,2:undefined};
var example2 = {8:null,0:null,2:null};

var keys = [];
for(var name in example1) {
   keys.push(name);
}
// keys = ["8","0","2"];

var otherKeys = Object.keys(example2);
// otherKeys = ["8","0","2"];

Setting the keys dynamically

var hh = 9;
var mm = 8;
var qty = 2;

var obj = {};
obj[hh] = null;
obj[mm] = null;
obj[qty] = null;

//obj = {9:null,8:null,2:null};

I'm not certain if this solves your problem or answers your question entirely but it might give you some more insight into what is happening and why. The above examples are a common way to create a quick lookup versus a dictionary with would have values in place of null or undefined.

0

You example uses a new feature of ECMAScript 6 that is the shorthand syntax for initialising object properties. This line in your example:

data.push({hh,mm,qty});

is equivalent to this verbose one:

data.push({hh: hh, mm: mm, qty: qty});

An object in JavaScript will always have keys and values. There is no way to save just values in a plain object. However, there are two other solutions.

One is using an array:

data.push([hh, mm, qty]);

Note the square brackets substituting the curly ones. This will obviously push an array of three values onto the data array. When retrieving the values, you can just refer to their index, as an array's items will always retain their indices:

var data2 = [hh, mm, qty];
var hh2 = data2[0];
var mm2 = data2[1];
var qty2 = data2[2];

Another way of just "saving the values" is using a set, though the construction of a Set object will still require passing it an array:

data.push(new Set([hh, mm, qty]));

Accessing the data is less straightforward in this case, as the set will typically only let you iterate it. Unlike similar data structures in other languages, a JavaScript set will retain the order of inserted values. It can therefore be safely converted into an array:

var mySet = new Set([hh, mm, qty]);
var data3 = Array.from(mySet);
var hh3 = data3[0];
var mm3 = data3[1];
var qty3 = data3[2];

You can read more about sets here.

mingos
  • 21,858
  • 11
  • 68
  • 105
0

You can wrap it over another JSON object with a key I assume you want a JSON object.

Like this { [{8,0,2}, {20,0,2}] } but this with a problem - It is not a valid JSON.

I had a similar problem for one of my scenario. Then I realised

A top level JSON can't exist without a key!

Consider this example, you have another KV pair in JSON and also this array.

{ 
  "somekey" : "somevalue",
  [ {8,0,2}, {20,0,2} ]
}

You can fetch "somevalue" with the key "somekey". But how would you access the array? you can't :(

I would suggest you to use a top level key for the JSON and make this array as value of it's. Example:

{
  "my array" : [ {8,0,2}, {20,0,2} ]
}