0

I have an Object which contains values as follow

[{"text":"Tag1"},{"text":"Tag2"},{"text":"Tag3"}] 

These are in the variable autosuggest. Now I want to get only the values

Tag1, Tag2, Tag3 

I´ve tried to do this

var textOnly = autosuggest.text 

But then, I get an "undefined" of

var textOnly = autosuggest[0] 

Then I get only the first string, 'Tag1'

Thank you for your tips

2
  • You would need to iterate over this. Commented Jun 21, 2015 at 4:34
  • 1
    @Paulpro: Make it an answer ( I reopened the question, the duplicate was horrible, but this must have been asked before...) Commented Jun 21, 2015 at 4:39

3 Answers 3

3

You can use Array.prototype.map to iterate through the array and get each elements text property:

var result = autosuggest.map( function( tag ) { return tag.text; } ); 
Sign up to request clarification or add additional context in comments.

Comments

1

If you're saying you want to get a string that is a comma-separated list of the values, then this will do it:

var textOnly = autosuggest.map(function(el){ return el.text; }).join(", "); // "Tag1, Tag2, Tag3" 

If you want to get an array that contains three elements, each of which being a string with one tag name in it, then leave off the .join() part:

var textOnlyArray = autosuggest.map(function(el){ return el.text; }); // ["Tag1", "Tag2", "Tag3"] 

More information at MDN:

1 Comment

Great! the textOnly-Version rocks! Thank you nnnnn
0

Iterate through autosuggest:

autosuggest.forEach(function(tag){ console.log(tag.text); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.