假设我有一个像这样的数组:
var ARTISTS: Artist[] = [
{
"name": "Barot Bellingham",
"shortname": "Barot_Bellingham",
"reknown": "Royal Academy of Painting and Sculpture",
"bio": "Some bio here...",
"friends": "James, Harry, Bob"
}
是否可以将键“ friend ”的值显示为无序列表,其中每个 friend 都有自己的列表项,例如:
<ul>
<li>James</li>
<li>Harry</li>
<li>Bob</li>
</ul>
我确实意识到“ friend ”最好存储为嵌套数组,以便显示为列表,但我正在将此作为另一个问题的潜在解决方法。
谢谢!
附注我正在使用 Angular 2。
最佳答案
您可以使用String.prototype.split()结交一群 friend 。
var arrayOfFriends = Artist[0].friends.split(", ");
// arrayOfFriends = ['James', 'Harry', 'Bob']
从那里循环数组并创建列表
关于javascript - 如何将逗号分隔的 JSON 值显示为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729743/