Compress 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 compress this array of RowDataPacket objects into a single object. After compression, 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 _IDand _NAMEhave the same value in the array of RowDataPacket objects. Other keys will have different meanings. How can this be done in node.js v4.7.x?

EDIT: This is not an array of json objects, as stated in the original question. This is an array of RowDataPacket objects that is the result of a MySQL query.

+4
source share
2 answers

.
:

    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);
Hide result

:

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);
Hide result
+1

, ,

{
  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
  }
}

,

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
}];

.

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]}}
+1

Source: https://habr.com/ru/post/1650649/


All Articles