user6064424
user6064424

Reputation:

Shrink this array of RowDataPacket objects into a single object

I am using node.js

I have this array of RowDataPacket objects below;

{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 1,
    _CASE_ID: 1
},
{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 2,
    _CASE_ID: 2
}
{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 3,
    _CASE_ID: 3
}

I would like to shrink this array of RowDataPacket objects into a single object. After shrinking, it will look like this;

{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 1,
    _CASE_ID: 1,
    _CASE_COUNTER: 2,
    _CASE_ID: 2,
    _CASE_COUNTER: 3,
    _CASE_ID: 3 
}

The keys _ID and _NAME have the same values in the array of RowDataPacket objects. The other keys will have different values. How can this be done in node.js v4.7.x?

EDIT: It is not an array of json objects as mentioned in the original question. It is an array of RowDataPacket objects which is the result of a MySQL query.

Upvotes: 4

Views: 1227

Answers (2)

Haresh Vidja
Haresh Vidja

Reputation: 8496

You can not use same index in one object, but you can achieve it in different way shrinked data format should be as mentioned below

{
  4:{   //here 4 is _ID
    _NAME:"Switch4",
    _CASE_COUNTER:[1,2,3], // array of all _CASE_COUTER for same _ID
    _CASE_ID:[1,2,3]     // array of all _CASE_IDfor same _ID
  }
}

Lets assume your data structure is

var data=[{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 1,
    _CASE_ID: 1
},
{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 2,
    _CASE_ID: 2
},
{
    _ID: 4,
    _NAME: "Switch4",
    _CASE_COUNTER: 3,
    _CASE_ID: 3
}];

You can shrink data in below way

shrink_data={};
for(i=0 ; i < data.length; i++){
  if(typeof shrink_data[data[i]._ID] == "undefined")
  {
    shrink_data[data[i]._ID]={
      _NAME: data[i]._NAME, 
      _CASE_COUNTER: [data[i]._CASE_COUNTER],
      _CASE_ID: [data[i]._CASE_ID] 
    };
  }
  else
  {
    shrink_data[data[i]._ID]._CASE_COUNTER.push(data[i]._CASE_COUNTER);
    shrink_data[data[i]._ID]._CASE_ID.push(data[i]._CASE_ID);
  }
}
console.log(JSON.stringify(shrink_data)); 
// {"4":{_NAME:"Switch4",_CASE_COUNTER:[1,2,3],_CASE_ID:[1,2,3]}}

Upvotes: 1

vp_arth
vp_arth

Reputation: 14982

Objects can't to have several same keys.
I advice you to collect this data to arrays:

    function combine(list) {
      return list.reduce((carry, o) => {
        for (var k in o) {
          if (!carry[k]) carry[k] = [];
          carry[k].push(o[k]);
        }
        return carry;
      }, {});
    }
    var input = [];
    input.push({id: 1, tag: 'Tag#1'});
    input.push({id: 2, tag: 'Tag#2'});
    input.push({id: 3, tag: 'Tag#3'});
    var res = combine(input);
    console.log(res);

To different behavior for different keys we should provide some schema:

function combine(list, schema) {
      return list.reduce((carry, o) => {
        for (var k in o) {
          switch (schema[k]) {
            case 'scalar': carry[k] = o[k]; break;// Overwrite
            default:
            case 'array': 
              if (!carry[k]) carry[k] = [];
              carry[k].push(o[k]);
              break;
          }
        }
        return carry;
      }, {});
    }
    var input = [];
    input.push({id: 1, tag: 'Tag#1'});
    input.push({id: 1, tag: 'Tag#2'});
    input.push({id: 1, tag: 'Tag#3'});
    var res = combine(input, {id: 'scalar', tag: 'array'});
    console.log(res);

Upvotes: 1

Related Questions