I am working on javascript object. And I want to pass data to next array in a specific sequence. like I get data like this
var obj = [{
0: "Owner Name"
1: "Mailing Address"
2: "Situs Address"
3: "APN"
}]
And want this sequence to be like this
var obj = [{
3: "APN"
0: "Owner Name"
2: "Situs Address"
1: "Mailing Address"
}];
Is it possible to do that, on bases of data? As I don't know from database sequence maybe change. edit: For those who are saying I should use array. I am getting data on basis of this object like,
var data[{
0:"CHANCE WILLIAM C & KATHRYN L"
1:"P O BOX 7642 CHICO CA 95927"
2:"LOT 4 PM 150-99/100"
3:"040-310-086-000" }]
I am working on javascript object. And I want to pass data to next array in a specific sequence. like I get data like this
var obj = [{
0: "Owner Name"
1: "Mailing Address"
2: "Situs Address"
3: "APN"
}]
And want this sequence to be like this
var obj = [{
3: "APN"
0: "Owner Name"
2: "Situs Address"
1: "Mailing Address"
}];
Is it possible to do that, on bases of data? As I don't know from database sequence maybe change. edit: For those who are saying I should use array. I am getting data on basis of this object like,
var data[{
0:"CHANCE WILLIAM C & KATHRYN L"
1:"P O BOX 7642 CHICO CA 95927"
2:"LOT 4 PM 150-99/100"
3:"040-310-086-000" }]
Share
Improve this question
edited Jul 7, 2017 at 7:47
ephemeral
asked Jul 7, 2017 at 7:37
ephemeralephemeral
4372 gold badges7 silver badges18 bronze badges
6
- 2 The properties in an object are not, and can not be, ordered in any way, so what you're trying to do is not possible. If you want to have a guaranteed order to the values put them in an array instead – Rory McCrossan Commented Jul 7, 2017 at 7:38
- The only way is to build another Map object by specifying keys & values in your desired order. But, why do you want to do this? Ordering of keys doesn't affect how you access the values.. – Chris Lam Commented Jul 7, 2017 at 7:39
- @ChrisLam Actually I have to pass a specific sequence to JSPDF to generate a pdf in a specific standard format. So I have to make a specif sequence – ephemeral Commented Jul 7, 2017 at 7:41
- Possible duplicate of Does JavaScript Guarantee Object Property Order? – str Commented Jul 7, 2017 at 7:43
- You could retrieve them in order, using an array for the keys – dev8080 Commented Jul 7, 2017 at 7:43
3 Answers
Reset to default 3Object not have sorting property
so you can do like this,
Covert the array object arguments into individual object.then sorting with dec
order of the key
var obj = [{
'0': "Owner Name",
'1': "Mailing Address",
'2': "Situs Address",
'3': "APN" ,
}];
var array = Object.keys(obj[0]).map(function(a){
return ({[a]:obj[0][a]});
}) //recreate with individual object
var sorted= array.sort((a,b)=> Object.keys(b)-Object.keys(a))
console.log(sorted) //sorted object
var res = sorted.map(a=> Object.values(a)[0]) // for array
console.log(res)
The properties in an object are not, and can not be, ordered in any way, so what you're trying to do is not possible. If you want to have a guaranteed order to the values put them in an array instead.
For those who are saying I should use array. I am getting data on basis of this object
In this case you can use Object.keys()
and map()
to convert the object to an array:
var data = [{
0: "CHANCE WILLIAM C & KATHRYN L",
1: "P O BOX 7642 CHICO CA 95927",
2: "LOT 4 PM 150-99/100",
3: "040-310-086-000"
}];
var arr = Object.keys(data[0]).map(function(k) {
return data[0][k];
});
console.log(arr);
The keys in a JavaScript object do not have any order. They will be displayed in any order and can change because order is not preserved.
If you need your data in a particular order, use an Array.
Example of an array of strings preserving order
var orderedItems = [
"3: APN",
"0: Owner Name",
"2: Situs Address",
"1: Mailing Address"
];