如何从打字稿中的数组中获取值?

时间:2017-09-30 15:29:01

标签: javascript angularjs typescript

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

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

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

4 个答案:

答案 0 :(得分:4)

您可以使用forEach循环:

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);

或者使用地图,但它可以在ES6之后的新浏览器中使用:

commentArray = array.map(function(object) {
    return object.Comments;
});
console.log("This is comment array... ", commentArray);

答案 1 :(得分:0)

只要TGW的答案是"正确"并且工作,我认为你应该知道并使用...(和... in)构造,它优于Array.forEach(更快,你可以使用continue和break关键字,更好的可读性并且更多时候你想迭代你的数组并用它来做事情,而不仅仅是得到一个属性(在这种情况下,Array.map非常适合:))

答案 2 :(得分:0)

你可以试试这个:

我们也可以在这里使用过滤器

library(tidyverse)
df3 <- left_join(nest(df1, -a), nest(df2, -a), by = "a")
df3
#>   a     data.x        data.y
#> 1 1 11, 12, 13 101, 102, 103
#> 2 2 21, 22, 23 201, 202, 203
#> 3 3 31, 32, 33 301, 302, 303

答案 3 :(得分:0)

您可以使用Underscore JS获得简短且更好的性能,如下所示:

    let ids: Array<number> = _.map(this.mylist, (listObj) => {
        return listObj.Id;
    });
相关问题