如何在javascript中从数组中获取值?

Cod*_*n03 0 javascript

我有一个看起来像这样的数组

   const array: any[] = []
   array.push({ 'Comments': this.comment, 'Name': this.name, 'Description' : this.description })
Run Code Online (Sandbox Code Playgroud)

我将该数组传递回父组件。如何获取评论中的值?

TGW*_*TGW 5

您可以使用 forEach 循环:

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);
Run Code Online (Sandbox Code Playgroud)

或者使用map,但它可以在新的浏览器中工作,可能是ES6之后的浏览器:

const commentArray = array.map(function(object) {
    return object.Comments;
});
console.log("This is comment array... ", commentArray);
Run Code Online (Sandbox Code Playgroud)