0

How you Convert this:

var values = [
    {name:'demian', email: 'demian@gmail.com', ID: 1},
    {name:'john'  , email: 'john@gmail.com'  , ID: 2},
    {name:'mark'  , email: 'mark@gmail.com'  , ID: 3},
    {name:'pete ' , email: 'pete@gmail.com'  , ID: 4}
];

To this:

var values = [
    {'demian', 'demian@gmail.com', 1},
    {'john'  , 'john@gmail.com'  , 2},
    {'mark'  , 'mark@gmail.com'  , 3},
    {'pete ' , 'pete@gmail.com'  , 4}
];
Merlin
  • 24,552
  • 41
  • 131
  • 206
  • 2
    Your second version is not valid JSON -- did you mean it to go from an array of objects to an array of arrays (i.e., in the second version, the curly braces would turn into square braces)? – Jacob Mattison Mar 20 '13 at 14:56
  • 3
    Are these actually JOSN strings, or is this just javascript you are working with? Also, are you sure the second example is what you want? because it doesn't look valid to me, perhaps you are trying to get an array of arrays? – musefan Mar 20 '13 at 14:56
  • 1
    You can't. An object has properties that values are stored against, you can't remove them. You could convert it to an array but I really don't see why you'd want to; working with an object with named properties rather than an array with numerical indices is much easier. – Anthony Grist Mar 20 '13 at 14:57
  • @AnthonyGrist: I trying to insert array into database. So trying to get into a form for "insert into table values "array". – Merlin Mar 20 '13 at 15:01
  • This is the related question: http://stackoverflow.com/questions/15367477/fastest-way-to-json-object-into-mysql-using-node – Merlin Mar 20 '13 at 15:02

3 Answers3

4

Here is a very simple solution that gets all values (as array) from a list of objects:

var data = [
  {name:'demian', email: 'demian@gmail.com', ID: 1},
  {name:'john'  , email: 'john@gmail.com'  , ID: 2},
  {name:'mark'  , email: 'mark@gmail.com'  , ID: 3},
  {name:'pete ' , email: 'pete@gmail.com'  , ID: 4}
]
var values = data.map( function(d) { return [d.name, d.email, d.ID] })
console.log(values)
// output on console:
// [
//   ["demian", "demian@gmail.com", 1],
//   ["john",   "john@gmail.com",   2],
//   ["mark",   "mark@gmail.com",   3],
//   ["pete ",  "pete@gmail.com",   4]
// ]

I hope this helps.

Edit - Here is a more generic solution:

values = data.map( function(d){ var a=[]; for(k in d) a.push(d[k]); return a })
console.log(values)

But then you need to make sure that you do not have additional properties on your objects (e.g., introduced by some JS framework). I prefer the explicit way.

Juve
  • 10,584
  • 14
  • 63
  • 90
2

For a general usage, when you not know the named-keys, or they are not the same in all the objects, you can use this script:

<script type="text/javascript">
var values = [
    {name:'demian', email: 'demian@gmail.com', ID: 1},
    {name:'john'  , email: 'john@gmail.com'  , ID: 2},
    {name:'mark'  , email: 'mark@gmail.com'  , ID: 3},
    {name:'pete ' , email: 'pete@gmail.com'  , ID: 4}
];

var nrv = values.length;
var values2 = [];           // to store the values, without named-keys

for(var i=0; i<nrv; i++) {
  values2[i] = [];
  for each(var val in values[i]) {
    values2[i].push(val);
  }
}

console.log(values2);

/* output on console
[["demian", "demian@gmail.com", 1],
["john", "john@gmail.com", 2],
["mark", "mark@gmail.com", 3],
["pete ", "pete@gmail.com", 4]]
*/
</script>
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
1

Try this:

var val_array = [];
for(var i in values)
{
    val_array.push(    
        [values[i].name, values[i].email, values[i].ID]     
    )
}
xdevel2000
  • 20,780
  • 41
  • 129
  • 196