Rock
Rock

Reputation: 758

How to Reorder the Array of object properties using javascript or lodash

i am having the array of objects That i am Going to Write into the Excel file. I am having soo many array of objects like this

[{'FirstName':'abc','Reg-no':1234,'branch':'mech','ReportDate':'11/12/2014','LastName':'HR'},{'FirstName':'xyz','Reg-no':1235,'branch':'CS','ReportDate':'11/12/2015','LastName':'BC'},{'FirstName':'pqr','Reg-no':1236,'branch':'IS','ReportDate':'11/12/2016','LastName':'TY'}]

I want to reorder the objects properties like this so that in the Excel First column it Starts from the reg No.

 [{'Reg-no':1234,'ReportDate':'11/12/2014','FirstName':'abc','LastName':'HR','branch':'mech'},
     {'Reg-no':1235,'ReportDate':'11/12/2015','FirstName':'xyz','LastName':'BC','branch':'CS'},
     {'Reg-no':1236,'ReportDate':'11/12/2016','FirstName':'pqr','LastName':'TY','branch':'IS'}
     ]

Can anybody Help me on this.

Upvotes: 1

Views: 1857

Answers (5)

Roy Sharon
Roy Sharon

Reputation: 3518

As others have pointed here, according to the Ecmascript standard the order of keys in an object is not fixed. However, most of the javascript engines iterate over an object's keys in the order of their creation. So you could do something like this:

function reorder(obj, keys) {
    return keys.reduce((newObj, key) => {
        newObj[key] = obj[key];
        return newObj;
    }, {});
}

With this, calling reorder({ a: 3, b: 5, c: 6}, ['b', 'c', 'a']) will most likely give you { b: 5, c: 6, a: 3 }.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

This is a proposal with an array. The first element is an array with the column description. The rest of the elements contain the wanted items with the wanted order.

var data = [{ 'FirstName': 'abc', 'Reg-no': 1234, 'branch': 'mech', 'ReportDate': '11/12/2014', 'LastName': 'HR' }, { 'FirstName': 'xyz', 'Reg-no': 1235, 'branch': 'CS', 'ReportDate': '11/12/2015', 'LastName': 'BC' }, { 'FirstName': 'pqr', 'Reg-no': 1236, 'branch': 'IS', 'ReportDate': '11/12/2016', 'LastName': 'TY' }],
    keys = ['Reg-no', 'ReportDate', 'FirstName', 'LastName', 'branch'],
    array = [keys].concat(data.map(function (a) {
        return keys.map(function (k) {
            return a[k];
        });
    }));


console.log(array);

Upvotes: 1

Redu
Redu

Reputation: 26161

I think one way of doing this job is using the new Map object. It will keep the order of insertion of the key value pairs. You can easily access them separately (Map.prototype.keys() or Map.prototype.values()) or together (Map.prototype.entries()) in the in the order they are inserted and utilize the Map the way you like. Let's see...

var data = [{'Reg-no':1234,'ReportDate':'11/12/2014','FirstName':'abc','LastName':'HR','branch':'mech'},
            {'Reg-no':1235,'ReportDate':'11/12/2015','FirstName':'xyz','LastName':'BC','branch':'CS'},
            {'Reg-no':1236,'ReportDate':'11/12/2016','FirstName':'pqr','LastName':'TY','branch':'IS'}],
datamaps = data.map(o => new Map().set("Reg-no"    ,o["Reg-no"])
                                  .set("ReportDate",o.ReportDate)
                                  .set("FirstName" ,o.FirstName)
                                  .set("LastName"  ,o.LastName)
                                  .set("branch"    ,o.branch));
console.log(...datamaps[0].entries());

Upvotes: 0

stdob--
stdob--

Reputation: 29172

You can use the header template to reorder. For example:

var headerTemplate = [ 'A', 'B', 'C', 'D' ];

// Unsorted data
var data = [ { C: '1:4', A: '1:3', B: '1:2', D: '1:1' },
             { B: '2:4', C: '2:3', D: '2:2', A: '2:1' },
             { C: '3:4', B: '3:3', D: '3:2', A: '3:1' } ]

var reorderedData = [];
while (data.length>0) {
  var row = data.shift();
  var reorderedRow = [];
  headerTemplate.forEach( function(ch) {
    reorderedRow.push( row[ch] );
  });
  reorderedData.push( reorderedRow );
}
console.log(reorderedData) // ==>

// [ [ '1:3', '1:2', '1:4', '1:1' ],
//   [ '2:1', '2:4', '2:3', '2:2' ],
//   [ '3:1', '3:3', '3:4', '3:2' ] ]

Upvotes: 0

Adam
Adam

Reputation: 5233

You can not order the properties of a javascript object, but you can structure your data into an array:

[
    [
        {name: 'Reg-no', value: 1234},
        {name: 'ReportDate', value: '11/12/2014'},
        {name: 'FirstName', value: 'abc'},
        {name: 'LastName', value: 'HR'},
        {name: 'branch', value: 'mech'}
    ],
    [
        {name: 'Reg-no', value: 1235},
        {name: 'ReportDate', value: '11/12/2014'},
        {name: 'FirstName', value: 'abc'},
        {name: 'LastName', value: 'HR'},
        {name: 'branch', value: 'mech'}
    ],
    [
        {name: 'Reg-no', value: 1236},
        {name: 'ReportDate', value: '11/12/2014'},
        {name: 'FirstName', value: 'abc'},
        {name: 'LastName', value: 'HR'},
        {name: 'branch', value: 'mech'}
    ]
]

Upvotes: 0

Related Questions