const commentArray = [];
array.forEach(function(object) {
var comment = object.Comments;
commentArray.push(comment);
});
//You can store all comments in another array and use it...
console.log("This is comment array...", commentArray);
let commentArray:any[];
array.filter(function(object) {
var comment = object.Comments;
commentArray.push(comment);
});
//Store here and use this.
console.log("This is comment array...", commentArray);
4条答案
按热度按时间zf9nrax11#
您可以使用forEach循环:
或者使用MAP,但它将在新的浏览器中运行,可能是ES6之后的浏览器:
edqdpe6u2#
只要TGW的答案是“正确的”并且有效,我认为您应该知道并使用for...of(和for...in)构造,它优于Arra.Each(速度更快,您可以使用Continue和Break关键字,更好的可读性),而且更多情况下,您希望迭代数组并使用它来做事情,而不是只获得一个属性(在本例中,Array.map非常适合:)
xoefb8l83#
您可以尝试这样做:
我们也可以在这里使用过滤器。
j91ykkif4#
您可以使用下划线JS来获得更短、更好的性能,如下所示: