1

I have an array of objects like so:

[
 0:'some value'
 1:'some value'
 3:'some value'
]

and what i want is a nice array of objects without keys for an editabale select like so:

       [
          {value: 1, text: 'Active'},
          {value: 2, text: 'Blocked'},
          {value: 3, text: 'Deleted'}
       ]

i have tried looping and assigning, but i am getting the same result. how can i achieve this array:

cities.push({value:value, text:value});
1
  • Your "array of objects" isn't valid syntax. What exactly do you have to begin with? What do you mean by "without keys"? Arrays and objects always have keys/indexes. Commented Sep 2, 2015 at 16:51

1 Answer 1

1

Your initial 'array of objects' doesn't make sense...do you mean those are the properties of a given single object?

If so, what you need to do is use a for in loop to loop over the objects properties, e.g.

for (var property in obj) {
  cities.push({value: property, text: obj[property]});
}
Sign up to request clarification or add additional context in comments.

3 Comments

Be careful with for .. in. If you are using it to loop over an array, then you are gonna get all the other properties of the array too, like length. Use a "normal" for loop (or arr.forEach()) for looping over an array.
From my understanding of his question, he does not have an array, but an object which happens to be in an array-like structure. That may be wrong...as we both previously mentioned his 'array of objects' is unclear.
I agree with your thought, I just wanted to warn about for .. in on arrays vs on objects.

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.