javascript - 过滤函数不返回值

标签 javascript

我会使用 JS 中的过滤器来过滤对象列表

let dataid = infosStructured.filter(elm => {
  console.log(elm.identifiant);
  console.log("matching value", elm.identifiant == this.dataService.getcurrentId()); // I get a true value
  elm.identifiant == this.dataService.getcurrentId();
});
console.log(dataid); // empty

我的 dataid 为空,而 elm.identifiant == this.dataService.getcurrentId() 在给定时刻为 true

最佳答案

它与函数式编程无关。尝试下面的代码

let f = a => { a == 1 };
f(1); // undefined
let g = a => a == 1;
g(1); // true
let h => a => { return a == 1; };
h(1); // true

最好不要使用内联函数来调试高阶函数。

关于javascript - 过滤函数不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669283/

相关文章:

javascript - 在 alpine.js 中单击事件后触发更改事件

javascript - 如何在页面加载后立即显示随机报价?

javascript fadein/fadeout,在 firefox 顶部淡出 img flicks

javascript - 何时在NavigationTiming对象中填充redirectStart?

JavaScript 捕获事件监听器和 MutationObserver

javascript - 同时控制 2 个 div 的可见性

javascript - 需要默认 CSS 按钮 (JQuery)

javascript - AngularJS Controller 变量未传递给子指令

javascript - meteor ,ClientStorage-TypeError : Cannot read property 'has' of undefined

javascript - 如何隐藏未选中的表单输入字段并仅显示已选中的输入字段