从列表末尾过滤元素(Filter elements from the end of a list)
我有一个如下所示的数据结构:
[(1, 2), (2, 3), (4, 0), (5, 10), (6, 0), (7, 0)]
在第二个元素为0的列表末尾只过滤元组的最佳方法是什么?
期望的输出是:
[(1, 2), (2, 3), (4, 0), (5, 10)]
I have a data structure that looks like this:
[(1, 2), (2, 3), (4, 0), (5, 10), (6, 0), (7, 0)]
What is the best way to filter out only tuples at the end of the list where the second element is 0?
The desired output is:
[(1, 2), (2, 3), (4, 0), (5, 10)]
原文:https://stackoverflow.com/questions/15695915
最满意答案
你滥用继承权。 用户组件没有理由扩展细节。 用户不是细节。 以下是您需要遵循的重要步骤:
重命名组件。 您的用户组件似乎实际上是ArtistListComponent。 您的详细信息组件实际上似乎是ArtistDetailComponent。 使用好名称更多地显示了继承不适用的方式:艺术家列表不是艺术家详细信息。
在您的艺术家列表组件组件中,添加名为
detailedArtist
的字段。 单击列表中的某个艺术家时,将detailedArtist
设置为单击的艺术家。在艺术家列表组件的模板中,仅在设置了详细艺术家时显示详细艺术家,并将该详细艺术家作为输入传递给ArtistDetailComponent:
<artist-detail *ngIf="detailedArtist" [artist]="detailedArtist"></artist-detail>
在ArtistDetailComponent中添加
@Input()
decorated属性。 使用setter,您可以知道艺术家何时作为输入更改,并因此可以从后端加载该艺术家的详细信息:@Input() set artist(a: Artist) { // load the details of the given artist }
You're misusing inheritance. There is no reason for the user component to extend detail. A user is not a detail. Here's the important steps you need to follow:
Rename your components. Your user component seems to actually be an ArtistListComponent. Your detail component seems to actually be an ArtistDetailComponent. Using good names shows even more how inheritance is not applicable: an artist list is not an artist detail.
In your artist list component component, add a field named
detailedArtist
. When clicking one of the artists in the list, set thedetailedArtist
to the clicked artist.In the template of the artist list component, display the detailed artist only if the
detailedArtist
is set, and pass that detailed artist as input to the ArtistDetailComponent:<artist-detail *ngIf="detailedArtist" [artist]="detailedArtist"></artist-detail>
In your ArtistDetailComponent add an
@Input()
-decorated property. Using a setter, you can know when the artist passed as input changes, and can thus load the detail of that artist from your backend:@Input() set artist(a: Artist) { // load the details of the given artist }
相关问答
更多-
正如你已经注意到的那样,lambda函数在定义lambda函数的时候捕获this函数。 这可能会导致更奇怪的结果( var _this = this也有一些 错误 ),尽管其中大多数似乎是固定的。 整个语言结构似乎是为了欺骗JavaScript范围( this是函数被调用的对象)。 至于你解释的怪异(this.active和_this.active的行为不同),在问题的范围内没有任何解释。 生成的JavaScript(如您所述)应该(几乎)相同。 除了写var _this = this; 导致_this在同 ...
-
如何在另一个模板中动态嵌入谷歌地图作为模板(How to embed google map dynamically as template in another template)[2021-10-21]
.heatmap需要一个高度,否则#map-canvas的90% - 高度将计算为0px .heatmap needs a height, otherwise the 90%-height of #map-canvas will be calculated as 0px -
正确的语法是: function questioner(question: QuestionResponse) { return `${question.questionText}`; } 这里的重要部分是使用`(反引号/反引号) 用'而不是'和''来包装你的字符串,以便使用${}语法。 这是一个TypeScript(和ES6)功能。 The correct syntax is: funct ...
-
一种解决方案可能是简单地将所有内容包装在UsersComponent模板中,而不是div的router outlet 。 然后,如果组件具有子UsersEditComponent (在这种情况下为UsersEditComponent ),则相应地隐藏/显示div 。 这样做是允许路由器在UsersEditComponent和UsersEditComponent之间保持正确的父/子层次结构,同时在激活该路由时仍然只显示UsersEditComponent内容。 这是UsersComponent一个实现,它完全 ...
-
Meteor:如何在其他模板标签中嵌入模板标签?(Meteor: How can I embed template tags within other template tags?)[2022-02-06]
也许almogdesign方法可以帮助你进一步...... https://github.com/aldeed/meteor-autoform/issues/483 Maybe almogdesign approach helps you further... https://github.com/aldeed/meteor-autoform/issues/483 -
希望这能帮到你: export enum SubstitutionType { REPLACEMENT_TEACHER_SUBJECT, REPLACEMENT_TEACHER, REPLACEMENT_FREE } 要在模板中使用它,您应该导出这样的类: export class Substitutions{ public substitutionsType: SubstitutionType; @Input() public set substitution(val: ...
-
我相信你会做这样的事情并在组件的ngOnInit()函数中初始化图表 import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-my-widget', templateUrl: './app/my-widget/my-widget.component.html', styleUrls: ['./app/my-widget/my-widget.component.css'] }) export c ...
-
你滥用继承权。 用户组件没有理由扩展细节。 用户不是细节。 以下是您需要遵循的重要步骤: 重命名组件。 您的用户组件似乎实际上是ArtistListComponent。 您的详细信息组件实际上似乎是ArtistDetailComponent。 使用好名称更多地显示了继承不适用的方式:艺术家列表不是艺术家详细信息。 在您的艺术家列表组件组件中,添加名为detailedArtist的字段。 单击列表中的某个艺术家时,将detailedArtist设置为单击的艺术家。 在艺术家列表组件的模板中,仅在设置了详细艺术 ...
-
TypeScript 0.9.1.1要求安装IE10或更高版本。 如果这不适合您,您可以通过节点运行tsc.js。 TypeScript 0.9.1.1 requires IE10 or later to be installed. If that's not an option for you, you can run tsc.js through node instead.