Home > Software design >  Create a new key and or array to existing object in node.js
Create a new key and or array to existing object in node.js

Time:12-23

I have an existing object called unsettled:

var unsettled =
    {
        processor: 1,
        dayFrom: 10,
        dayTo: 12
    }

Im trying to add an object called pName as I do with Angular, but it is not working.

unsettled.pname = "something"

It does work if unsettled.pname already exists on the object, but not if I want to create it.

Also, after a sql Query where I get several results, I need to create an array. Same thing, when I do

const pool = await utils.poolPromise
        const recordset = await pool.request()
            .query(sqlString)
        if (recordset.rowsAffected[0] > 0) {
            unsettled.processors = recordset.recordset;
        }

Is not working either (the array processors is not being created).

Thanks.

CodePudding user response:

For the array, try:

    if (recordset.rowsAffected[0] > 0) {
        unsettled["processors"] = recordset.recordset;
    }

As for as pName, please try

unsettled["pname"] = "something"
  • Related