2016-08-01 68 views
2

如何使用角度2方法重构此代码?我在谷歌上找不到任何东西。

var tooltipsData = $.grep(tooltips, function (element, index) { 
    return (element.ProductCode == ProductCode); 
}); 
+0

我不知道Angular1。你想达到什么目的? –

回答

3

看起来像什么你真的想要做的是落实,如果没有的jQuery(角2还仅仅是JavaScript的(或打字稿))。 如果你想实现它的JS,使用Array.filter function

var tooltipsData = tooltips.filter(function (element, index) { 
    return (element.ProductCode === ProductCode); 
}); 
+1

谢谢!这应该工作。我喜欢它在方法中使用JS bulit。 –

+0

可能是一个非问题,但认为值得一提的是浏览器支持只能追溯到IE9的过滤功能。 – eatinasandwich

2

Angular1使用jQuery:

var tooltipsData = $.grep(tooltips, function (element, index) { 
     return (element.ProductCode == ProductCode); 
    }); 

Angular2:

this.tooltipsData = tooltips.forEach((element, index)=>{ 
    return (element.ProductCode == ProductCode); 
}); 

您还可以使用

this.tooltipsData = tooltips.filter((element, index) => { 
    return (element.ProductCode == ProductCode); 
}); 
+1

谢谢!我喜欢使用lambda函数。我仍然习惯他们。 –

+1

'forEach'总是返回undefined,因此以这种方式使用它并不能解决问题,因为tooltipsData将始终是'undefined'。 –

+0

你一直未定义的含义是什么? – micronyks

相关问题