Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I can't seem to get typeahead to work with using key/value pairs. Since each key is different, and I want to only show the value as typeahead value.

Here is an example of an array I'm trying to use:

[
   {"1":"partner1"},
   {"2":"partner2"},
   {"3":"partner3"},
   {"4":"partner4"}
]

So, how should I go about iterating through each object and only grabbing the value for each (ie. "partner1") to display in the typeahead dropdown?

Thanks for your help!

share|improve this question

var data = [
 {"1":"partner1"},
 {"2":"partner2"},
 {"3":"partner3"},
 {"4":"partner4"}
];
for (var i = 0; i < data.length; i++) {
  data[i] = data[i][Object.keys(data[i])[0]];
}
console.log(data)

Summary:

For each object in the array, get the first key. Access the value using this key and overwrite the object with this value.

share|improve this answer
    
that looks incredibly painful. no easier way? – AlGoreRhythm Jan 24 at 18:02
    
There might be other ways to write it but at the end of the day they all have to boil down to this. You obviously have to iterate of the array (for loop or iterator). Then you have to figure out what key you want (there is only one so it is that one). Then you have to access the value indexed by that key. Then you have to save that value. There might be some libraries that can make the code shorter but they will end up doing the same thing, or something even worse. – Alec Fenichel Jan 24 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.