0

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.

Andrea
  • 11,801
  • 17
  • 65
  • 72
Veera Bhadra
  • 200
  • 3
  • 13
  • Please refer [enter link description here][1] [1]: http://stackoverflow.com/questions/10691409/add-new-element-at-the-beginning-of-a-json – syms Apr 22 '15 at 11:56
  • 3
    Object items are disordered. So there is nothing called *first element* in objects. – Lewis Apr 22 '15 at 11:57
  • What problem are you trying to solve by attempting this, because your understanding of JS objects is wrong. Perhaps we can help figure out what you're trying to do. – Andy Apr 22 '15 at 11:59
  • 1
    Syms I refered your link. Thanks for posting. My problem is different. In this select I wanna select "select" option and select option should be at the first of drop down. – Veera Bhadra Apr 22 '15 at 12:07
  • 1
    Tresdin - please give some references and explain your comment. In object, elements will maintain in disorder way? – Veera Bhadra Apr 22 '15 at 12:18
  • Babu Veera Konchem ardham ayyela cheppandi @Veera – Arepalli Praveenkumar Apr 22 '15 at 12:26
  • http://stackoverflow.com/questions/29031394/how-to-assign-selected-options-text-to-another-model-while-using-ng-options/29032227#29032227 Please Check the reference .Here is my Answer – Arepalli Praveenkumar Apr 22 '15 at 12:29

3 Answers3

0
var TICKET_PRIORITIES = {
    LOW: 'low',
    NORMAL: 'normal',
    HIGH: 'high',
    VERY_HIGH: 'very high'
  };
TICKET_PRIORITIES['SELECT']='select';
alert(JSON.stringify(TICKET_PRIORITIES));  

Demo

ozil
  • 6,930
  • 9
  • 33
  • 56
0

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';
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
0

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, e.g. 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"})
Community
  • 1
  • 1
helmbert
  • 35,797
  • 13
  • 82
  • 95