2017-02-23 28 views
0

我正在寻找筛选对象数组,并查找匹配对象日期或最近插入新对象之后的索引。通过数组筛选并找到拼接的索引

let expenseIndex = tempArray.findIndex((a: any) => a.Date <= expense.Date); 
tempArray.splice(expenseIndex, 0, expense); 

运营商<=似乎并没有在这里工作。如果我这样做==和费用与匹配的日期我找回索引,但没有匹配时,我得到0-1

回答

1

显示的当前类似的指数试试这个(假设临时数组按日期IST排序):

temp = temp 
    .filter(v => v.Date <= exp.Date) 
    .concat(exp, array.filter(v => v.Date > exp.Date)) 
0

angular在使用*ngFor指令时提供索引。

你可以得到以下

<div *ngFor="let item of items; let i = index;" 
    (click)="doSomethingWithIndex(i)"> 
    {{item.title}} 
</div> 
+0

的问题指出,我需要插入一个“新”对象放入数组中。您可以使用现有对象的示例。不管怎么说,多谢拉 –