0

hello I am new in node js. and i want to add a key with resultant array and print it in json array.

connection.modal.find( { 'id' : '2' }, function ( err, result ) {
    var response = {};

    result['add_key'] = 'abcdd';
    response['success'] = true;
    response['result'] = result;
    response['msg'] = 'Result fetched';
    res.json(response);
});

It prints without add_key

iam
  • 973
  • 4
  • 11
  • 21
  • Where is `result` declared? What type of variable is it? If it's an array, then properties of an array are not enumerated when turning into JSON. And, what exactly do you see in the JSON response? – jfriend00 Mar 08 '16 at 06:44
  • It is resultant array that is coming from query result . I updated my question. – iam Mar 08 '16 at 06:49
  • The type of `result` is `Array`, so is it right for `result['add_key'] = 'abcdd'`? – BlackMamba Mar 08 '16 at 06:53
  • i think you can read this link: http://stackoverflow.com/questions/16196338/json-stringify-doesnt-work-with-normal-javascript-array – BlackMamba Mar 08 '16 at 06:54

3 Answers3

0

The JSON array data type cannot have named keys on an array.

Normal JavaScript arrays are designed to hold data with numeric indexes. You can stuff named keys on to them (and this can be useful when you want to store metadata about an array which holds normal, ordered, numerically indexed data), but that isn't what they are designed for.

If you want named keys, use an Object, not an Array.

var test = {};           // Object
test['a'] = 'test';
test['b'] = [];          // Array
test['b'].push('item');
test['b'].push('item2');
test['b'].push('item3');
var json = JSON.stringify(test);
alert(json);
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • It is resultant array that is coming from query result . I updated my question. – iam Mar 08 '16 at 06:45
0

While a Javascript array can have custom properties such as you are doing with this line of code:

result['add_key'] = 'abcdd';

JSON.stringify() (and consequently res.json() too) will only put actual array elements (not custom properties) in the generated JSON. So, if result is an array, that is why this property does not show in the generated JSON.

In fact, the JSON text format, only has a place for array elements in the JSON format for an array. There is no place for regular custom properties like your ['add_key] property. That property would have to be on a plain object for it to show in the JSON.

In the section 7 of the JSON specification where arrays are described, it clearly shows that the only JSON representation for an array is the array elements themselves (separated by commas). There is no place for a property name/value pair in the expression for an array.


You did not show exactly what you want the resulting JSON to look like, but there are several other ways you could represent the add_key property and value. You could move the property to the response object:

connection.modal.find( { 'id' : '2' }, function ( err, result ) {
    var response = {};

    response['add_key'] = 'abcdd';
    response['success'] = true;
    response['result'] = result;
    response['msg'] = 'Result fetched';
    res.json(response);
});

You could put the result into it's own object and that object could have the add_key property on it:

connection.modal.find( { 'id' : '2' }, function ( err, result ) {
    var response = {};
    var resultContainer = {};

    resultContainer['add_key'] = 'abcdd';
    resultContainer['result'] = result;
    response['success'] = true;
    response['result'] = resultContainer;
    response['msg'] = 'Result fetched';
    res.json(response);
});

FYI, you don't have to normally use the bracket syntax for setting properties. You could also do this (which many find a bit cleaner):

connection.modal.find( { 'id' : '2' }, function ( err, result ) {
    var response = {};

    response.add_key = 'abcdd';
    response.success = true;
    response.result = result;
    response.msg = 'Result fetched';
    res.json(response);
});

The only time you have to use the bracket syntax is if the property name is in a string variable or if it contains certain characters that aren't permitted in the dot syntax. For regular alpha characters and a property name that is not in a variable, you can just use the dot syntax.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Nodejs doesn't have named indexed arrays (Associative arrays)

For the same Nodejs has Object data type use it

For your case


connection.modal.find( { 'id' : '2' }, function ( err, result ) {
    var response = {};

    // Converting array to object
    result = Object.assign({}, result);
    // Now you can add your required keys
    result['add_key'] = 'abcdd';

    response['success'] = true;
    response['result'] = result;
    response['msg'] = 'Result fetched';
    // Now response will have numeric keys and your required key also 
    // but data type will object
    res.json(response);
});

I hope this will helps

RameshN
  • 510
  • 7
  • 14