问题
Say I have an array like this:
var ARTISTS: Artist[] = [
{
"name": "Barot Bellingham",
"shortname": "Barot_Bellingham",
"reknown": "Royal Academy of Painting and Sculpture",
"bio": "Some bio here...",
"friends": "James, Harry, Bob"
}
Is it possible to display values for the key “friends” as an unordered list where each friend would be it’s own list item, e.g.:
<ul>
<li>James</li>
<li>Harry</li>
<li>Bob</li>
</ul>
I do realize the "friends" would be better stored as a nested array in order to display as a list, but I'm looking into this as a potential workaround for another issue.
Thank you!
P.S. I'm using Angular 2.
回答1:
You can use String.prototype.split() to get an array of friends.
var arrayOfFriends = Artist[0].friends.split(", ");
// arrayOfFriends = ['James', 'Harry', 'Bob']
From there loop over the array and create your list
来源:https://stackoverflow.com/questions/44729743/how-to-display-comma-delimited-json-value-as-a-list