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

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.

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.

Can't force object keys to be integer [duplicate]

This question already has answers here:
Keys in Javascript objects can only be strings?
(8 answers)
Closed 4 years ago.
const obj = {
15: 100
};
for(let key in obj)
console.log(key, typeof(key), typeof(+key))
The result is 15 string number. I'm trying to iterate over object values and put some of them into Map object but types compatibility seems unable to achieve. Am I doing something wrong here or object keys are always strings?
Object.keys(obj)
also returns ["15"]
Object keys are always strings. You can see more about it here:
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
For you to be able to achieve what you want you will need to cast the keys back to integers.

Javascript object traversal in wrong way [duplicate]

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

Counting length of another object property within object function -- JavaScript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I am new to JavaScript objects so please bear with me.
This is my JavaScript:
var dragSources = {
elementSources: {
squareSource: document.getElementById('squareSource'),
circleSource: document.getElementById('circleSource')
},
ifDragSource: function(elem) {
console.log(this.elementSources.length);
}
};
If you look at console.log(this.elementSources.length); you can probably tell that I am trying to get the length of the elementSources property. However, it returns undefined. What am I doing wrong?
It's an object, not an array, therefore it doesn't have a length property.
You could use Object.keys(this.elementSources).length to get the number of keys.
The .keys() method essentially returns an array of the object's keys.
In this case, Object.keys(this.elementSources) returns:
["squareSource", "circleSource"]
Then we are just getting the length of that array, which is 2.
var dragSources = {
elementSources: {
squareSource: document.getElementById('squareSource'),
circleSource: document.getElementById('circleSource')
},
ifDragSource: function(elem) {
console.log(Object.keys(this.elementSources).length);
}
};
dragSources.ifDragSource(); // 2
Technically if you need the length property in a code you have to store the length value directly in the object.
Using Object.key function decreases the code efficiency .
every time you invoke that function to access that value you have to re-run the same function again and again.
in order access to the length property in a js array, the BIG O is always equal to 1.
because when you update the array the length property would be updated consequently
But in that case the big O would be in the size of the Object and (O) = n

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