Javascript object traversal in wrong way [duplicate] - javascript

This question already has answers here:
How to keep an Javascript object/array ordered while also maintaining key lookups?
(2 answers)
Closed 7 years ago.
I have a json object in occupancy_list as follows :
I am traversing the object as follows :
for(var prop in occupancy_list)
{
console.log(prop);
}
I am getting values in reverse order. Like at first Room 2 then Room 1. How can I fix it ?

Object properties are unsorted and could always be random. If you need order - get the keys, and iterate them that way:
Object.keys(occupancy_list).sort(function(a, b) {
//sort logic
}).forEach(key) {
//logic on each key
});

Related

convert array(2) that has sorted object to object [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Closed 1 year ago.
I'm trying to sort an object from db
MyOjbect : {
key: {
val1:"value",
val2:"",
val3:time,
val4:...
...}
}
and I sorted the object by val3
const sorted = Object.entries(MyObject)
.sort((x,y) => {x[1].val3 - y[1].val3} );
and got an array type.
0:(2) ["key" , {val1:...val2:...}]
1:(2) ["key" , {val1:...val2:...}]
...
I tried reduce() to make it back to the object type, but it changes to be in unsorted order
How can I keep the order sorted and change the type to the object?
You can't. An object doesn't keep the sort order as an Array does it. If you want to have the keys sorted you need to use an Array.

Take a subset from a complex object in JS [duplicate]

This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 1 year ago.
How can I take (in JS) all the values inside this nested object except for the last two elements (scenario and total). here an image of the object I have:
I need an array equal to this one, but without "scenario" and "total" in the 3 nested objects inside.
Use map function
array.map(item => {
delete item.scenario;
delete item.total;
return item;
});

Sort v-for over an object (Vue2) [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Sort JavaScript object by key
(37 answers)
Closed 4 years ago.
I am using Vue2 and try to sort a v-for. I am iterating over an object and the vue-doc states:
When iterating over an object, the order is based on the key
enumeration order of Object.keys(), which is not guaranteed to be
consistent across JavaScript engine implementations.
My v-for without sorting looks like this:
<my-component :key="key + value + uid"
v-for="(value,key) in myObject" :key-prop="key"
:value-prop="value"></my-component>
How would I sort this if the Object.keys() are not consistent?
Edit: This is an example Object (returned from Rest-Service)
Chrome console logs this:
{
HUMID: "66%"
DAY: "true"
TEMP: "30"
}
The actual object is much larger.
Edit2:
With the help of "Oli Crt" I solved this. I wasn't able to calculate the sortedObjects as a computed property in my case, but I triggered it on getting the Response of my Rest-Service.
sortObject: function() {
this.myObjectSorted = Object.keys(this.myObject).sort();
}
This results in the following Object:
{
0: "DAY"
1: "HUMID"
2: "TEMP"
}
By taking the value of this sorted Object as my key-prop and getting the actual value (66%, true, 30) of the unsorted Object with
myObject[value]
I got the desired result.
<my-component :key="key + value + uid"
v-for="(value,key) in myObjectSorted" :key-prop="value"
:value-prop="myObject[value]"></my-component>
I think you should use a computed property.
You could sort the keys of your object
computed: {
myObjectSorted: function () {
return Object.keys(this.myObject).sort(...); // Do your custom sorting here
}
}
Then use myObject[key] with the key from myObjectSorted in v-for.

Trying to sort object turns into array object [duplicate]

This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 4 years ago.
This how the default api returns. I want to sort them with basis of min_conversion_count properties.
After sorting the structure changes which is affecting my code. How is it possible to sort them with the property and remain the same format.
Code to sort
var rewarddata = res1.response.reward_list;
// console.log(rewarddata);
sortable_data = [];
console.log(rewarddata);
for (var idx in rewarddata)
{
sortable_data.push([idx,rewarddata[idx]]);
sortable_data.sort(function(a, b) {
return a[1].min_conversion_count - b[1].min_conversion_count;
});
console.log(sortable_data);
}
After sorting it changes like this.
You started out with an object, and you put it into an array in order to sort it; once your array is sorted, you have to turn it back into an object.
const sortedObj = sortable_data.reduce((accum, [key, value]) => {
accum[key] = value;
return accum;
}, {});
Note that property ordering is generally not something to rely on. It would be better to save an array of the sorted keys only, and then use that array instead.

Sorting objects inside an array [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 8 years ago.
I have an array of objects, and i am basically breaking my head on sorting it,my array is something like dis :
var myArray = [{1:3},{3:19},{5:53},{6:26},{e:53},{c:107},{B: 2},{f: 5}];
But i have to sort this in such a way that my final output is something like this:
myArray = [{c:107},{5:53},{e:53},{6:26},{3:19},{f: 5},{1:3},{B: 2}];
i.e.; based on the value in each object of array element, the array should sorted in descending order.
Thank you in advance.
You can simply use a comparator function in Array.prototype.sort like this
console.log(myArray.sort(function(first, second) {
return second[Object.keys(second)[0]] - first[Object.keys(first)[0]];
}));
Since the key will be different in every object, we get the actual list of keys and taking only the first item in it.

Categories