I have an array of objects that I need to rearrange the columns and add a static value to each one:
var homes = [
{
"h_id": "3",
"city": "Dallas",
"state": "TX",
"zip": "75201",
"price": "162500"
}, {
"h_id": "4",
"city": "Bevery Hills",
"state": "CA",
"zip": "90210",
"price": "319250"
},...
];
The desired format is as follows:
var homes = [
{
"h_id": "3",
"price": "162500",
"zip": "75201",
"city": "Dallas",
"state": "TX",
"staticValue": "1234"
}, {
"h_id": "4",
"price": "319250",
"zip": "90210",
"city": "Bevery Hills",
"state": "CA",
"staticValue": "1234"
},...
];
I have attempted this approach:
let data = homes.map(function(x) {
return {h_id:x.h_id,
price : x.price,
zip : x.zip,
city : x.city,
state : x.state,
staticValue : "1234"}
});
However, the column order remains the same:
h_id,city,state,zip,price,staticValue
Instead of the desired order:
h_id,price,zip,city,state,staticValue
What could be causing this issue?
Edit
In reality, I am using this reordering method to send the object as a DataTable to a C# controller. Upon reaching the Controller, the ordering is correct. It seems that the map function used has functioned properly.
Here's a working example: jsfiddle.net/a6o8jbh9/1