1

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.

3
  • 3
    actually, you can not order the properties of an object. Commented Jun 17, 2016 at 6:51
  • thanks @NinaScholz for your reply. is there any Suggestion How We can modify that. Commented Jun 17, 2016 at 6:54
  • you could use an array instead with a defined order, like with the example [['abc, 1234, 'mech', '11/12/2014', /*...*/]] Commented Jun 17, 2016 at 6:57

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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' ] ]

Comments

0

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.