I have an object:
TICKET_PRIORITIES: {
LOW: 'low',
NORMAL: 'normal',
HIGH: 'high',
VERY_HIGH: 'very high'
}
In client side I have to add select at first.
Result I wanted to be
TICKET_PRIORITIES: {
SELECT: 'select',
LOW: 'low',
NORMAL: 'normal',
HIGH: 'high',
VERY_HIGH: 'very high'
}
<select ng-model="selectedPriority" ng-options="key as key | translate for (key, value) in priorities" class="form-control m-b"> </select>
In this select I wanna select "select" option and select option should be at the first of drop down.
var TICKET_PRIORITIES = {
LOW: 'low',
NORMAL: 'normal',
HIGH: 'high',
VERY_HIGH: 'very high'
};
TICKET_PRIORITIES['SELECT']='select';
alert(JSON.stringify(TICKET_PRIORITIES));
Object properties order are not concerned while creating object but you can create new properties like:
TICKET_PRIORITIES = {
LOW: 'low',
NORMAL: 'normal',
HIGH: 'high',
VERY_HIGH: 'very high'
}
TICKET_PRIORITIES.SELECT='select';
This is not possible . Javascript does not care about or enforce the order of properties in objects (see this question for reference). Or, to quote the ECMAscript standard (emphasis mine):
4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
If order is important to you, you will need a different data structure using arrays, eg something like this:
TICKET_PRIORITIES: [
{id: "LOW", label: "low"},
{id: "NORMAL", label: "normal"},
{id: "HIGH", label: "high"},
{id: "VERY_HIGH", label: "very high"}
]
This allows you to do something like this ( unshift
adds an element to the beginng of an array):
TICKET_PRIORITIES.unshift({id: "SELECT", label: "select"})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.