'how to add property to object to desired position when i have data like this in js
z = {
Asf:46,
sage:46,
fdfds:58,
};
z["619"] = 48;
console.log(z);
// I try to add to the bottom of the object but it add to the top the object it just because of the string passing in terms of numbers but I tried other solution it won't work
Solution 1:[1]
I try to add to the bottom of the object but it add to the top the object
The order of properties in an "ordinary" JavaScript object such as yours is set out by the specification. For the properties in your example, the 619 property comes first in that order, because it's an "array index" property name, those come before non-array index property names.
You can't change that, but more importantly, relying on object property order is almost always a bad idea. If you want order, use an array or a Map. For instance, in your case, you might want a Map, which is ordered purely by the order in which you add entries to it:
const map = new Map([
["Asf", 46],
["sage", 46],
["fdfds", 58],
]);
map.set("619", 48);
Live Example:
const map = new Map([
["Asf", 46],
["sage", 46],
["fdfds", 58],
]);
map.set("619", 48);
console.log([...map]);
.as-console-wrapper {
max-height: 100% !important;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
