0

I have a foreach loop and I'm adding key value pairs to my DBSchema, but when I use "DBSchema.push({key: DBSchemaTemp})", it does not put the real value of "key" and instead is pushing the word "key"; how can I solve the problem?

var DBSchema = [];
async.each(Object.keys(config.models), function(key) {
    var DBSchemaTemp = [];

    .
    .
     Working on DBSchemaTemp
    .
    .

    DBSchema.push({key: DBSchemaTemp});
});

The result is like the following:

[ { key: [ [Object], [Object], [Object], [Object] ] },
  { key: [ [Object] ] },
  { key: [ [Object] ] },
  { key: [ [Object], [Object], [Object], [Object] ] } ]

EDITED: Desired output:

{ name1: [ [Object], [Object], [Object], [Object] ] ,
   name2: [ [Object] ] ,
   name3: [ [Object] ] ,
   name4: [ [Object], [Object], [Object], [Object] ] }

or:

{ name1: { [Object], [Object], [Object], [Object] } ,
   name2: { [Object] } ,
   name3: { [Object] } ,
   name4: { [Object], [Object], [Object], [Object] } }

After using the solution by Hamilton:

var arrayInSchema = DBSchema[key];
if(typeof arrayInSchema === 'undefined') DBSchema[key] = [];
DBSchema[key].push(DBSchemaTemp);

  { user:
    [ [ Name: { AttributeName: 'Name', AttributeType: 'STRING' },
        Email: { AttributeName: 'Email', AttributeType: 'STRING' },
        Password: { AttributeName: 'Password', AttributeType: 'STRING' },
        role: { AttributeName: 'role', AttributeType: 'STRING' } ] ]
    name2:
    [ [ url: { AttributeName: 'url', AttributeType: 'STRING' } ] ]
    name3:
    [ [ Name: { AttributeName: 'Name', AttributeType: 'STRING' },
        Caption: { AttributeName: 'Caption', AttributeType: 'STRING' },
        url: { AttributeName: 'url', AttributeType: 'collection' } ] ]
    name4:
    [ [ media: { AttributeName: 'media', AttributeType: 'collection' } ] }

But there is a duplicate parenthesis! Also by "Is it also possible to merge with the previous result if already that key exist?" I mean if in another itteration again if we push the following, it should merge it with the previous result which already exist:

user: [ Address: { AttributeName: 'Name', AttributeType: 'STRING' }]

I guess the problem is duplicate paranthesis; I dont know how they are coming; If I did not have them, the following should work:

var arrayInSchema = DBSchema[key];
if (typeof arrayInSchema === 'undefined') {
    DBSchema[key] = [];
    DBSchema[key].push(DBSchemaTemp);
} else {
    DBSchema[key].concat(DBSchemaTemp);
}

Thanks

  • *"Also I was wondering how can I see the real content of object in command line? It is not showing now"* It depends on the browser, but `console.dir(value)` should work everywhere. – Felix Kling Aug 06 '14 at 21:21
  • Burt it is not working in my case! –  Aug 06 '14 at 21:23
  • 1
    Are you working with Node.js (since you said "command line")? In that case, Node.js won't log anything deeper than a certain level. You have to go a level deeper yourself, e.g. with `console.log(DBSchema[0])`. Or use something like https://github.com/node-inspector/node-inspector – Felix Kling Aug 06 '14 at 21:25
  • @FelixKling could you please see my edited question; I edited it now ; mentioned my desired out put; could you please help me with that please; appreciate your help in advance –  Aug 06 '14 at 22:05
  • How to do this is explained in the duplicate question. – Felix Kling Aug 06 '14 at 22:17
  • Or maybe the problem is that I'm using async.each and all of the arrayInSchema==='undefined', right? –  Aug 06 '14 at 23:26

1 Answers1

1

Try something like this:

var DBValue = [];
DBValue[key] = DBSchemaTemp;
DBSchema.push(DBValue);

Essentially, when using an object literal, the 'key' there is not considered a variable. By using bracket notation to set the value, it will be interpreted as a variable (rather than a string) and you should be good to go.

EDIT: To achieve your desired output, try something like this:

var arrayInSchema = DBSchema[key];
if(typeof arrayInSchema === 'undefined') DBSchema[key] = [];
DBSchema[key].push(DBSchemaTemp);
Hamilton Lucas
  • 419
  • 2
  • 5
  • *"Essentially, when using JSON the 'key' there is not considered a variable."* Luckily no one is using JSON here. The OP is using an *object initializer*, or *object literal*, which has nothing to do with JSON. Please don't call object literals "JSON". *"By using array access to set the value"* This way of accessing properties is called *bracket notation* and has nothing to do with arrays (unlike in other languages). However, it's true that bracket notation is the only way to access properties (elements) of arrays, because numbers are not valid identifier names, so dot notation doesn't work. – Felix Kling Aug 06 '14 at 21:19
  • Also I was wondering how can I see the real content of object in command line? It is not showing now –  Aug 06 '14 at 21:20
  • could you please see my edited question; I edited it now ; mentioned my desired out put; could you please help me with that please; appreciate your help in advance –  Aug 06 '14 at 22:06
  • 1
    See my edit. The structure you've created looks like a map of arrays. – Hamilton Lucas Aug 06 '14 at 22:17
  • I really appreciate your help! Your solution deserves more than one like! Is it also possible to merge with the previous result if already that key exist? –  Aug 06 '14 at 22:27
  • Does concat work in this case? –  Aug 06 '14 at 22:29
  • 1
    What do you mean by "merge"? It's difficult to say without knowing what the DBSchemaTemp object really looks like. [This post](http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) goes into detail about merging objects. – Hamilton Lucas Aug 06 '14 at 22:40
  • Or maybe the problem is that I'm using async.each and all of the arrayInSchema==='undefined', right? –  Aug 06 '14 at 23:26