0

I am refactoring some code, and want to change an array of code/name objects to be named objects. I want to do this so I can use it for equality tests. The old way was to compare the code directly...

if(current.addressCode === "1"){ ... }

But I would prefer to do it like this...

if(current.addressCode === types.student.address.letter.code){ ... }

The old object looks like this...

student{
  address: [
    {code: "0", name: "Select proof of address"},
    {code: "1", name: "Letter"},
    {code: "3", name: "Photograph"}
  ],
  id: [
    {code: "0", name: "Select type of ID"},
    {code: "1", name: "Passport"},
    {code: "2", name: "Driving Licence"}
  ]
}

So I want my new object to look like this...

student: {
  address: {
    select: {code: "0", name: "Select proof of address"},
    letter: {code: "1", name: "Letter"},
    photograph: {code: "3", name: "Photograph"},
  },
  id: {
    select: {code: "0", name: "Select type of ID"},
    passport: {code: "1", name: "Passport"},
    drivingLicense: {code: "2", name: "Driving Licence"},
  }
}

Where I am stuck, is I don't know how to generate a select element in angular js from this object which used to be generated from the old structure like this...

<select ng-model="current.addressCode" ng-options="t.code as t.name for t in student.address"></select>

How can I use the new object to generate a select form element?

1 Answer 1

3

Try this

<select ng-model="current.addressCode" ng-options="value.code as value.name for (key,value) in student.address"></select>
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea how to order the select by code? Here is a fiddle: jsfiddle.net/UPWKe/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.