简体   繁体   English

如何通过在 forEach 循环中使用键返回我想要的对象?

[英]How do I return the object I want by using a key in a forEach loop?

Consider this code:考虑这个代码:

const year = 1910;

const items = [
  {
    name: 'gallon of gas',
    year: 1910,
    price: .12
  },
  {
    name: 'gallon of gas',
    year: 1960,
    price: .30
  },
  {
    name: 'gallon of gas',
    year: 2010,
    price: 2.80
  }
]

How do I display the price of the object corresponding to the year defined above?如何显示与上面定义的年份对应的对象的价格?

items.forEach(d => {
 if (d.year === year) {
   return d.price;
   }
});

^ Why doesn't that solution work? ^ 为什么那个解决方案不起作用?

The forEach() function doesn't return a value, regardless of what you return in the callback function.无论您在回调函数中返回什么, forEach()函数都不会返回值。 Use find() instead to find the item that matches your criteria:使用find()来查找符合您条件的项目:

 const year = '1910'; const items = [ { name: 'gallon of gas', year: 1910, price: .12 }, { name: 'gallon of gas', year: 1960, price: .30 }, { name: 'gallon of gas', year: 2010, price: 2.80 } ]; const item = items.find(i => i.year == year); console.log(item.price);

Note: you can't use strict comparison ( === ) in the callback to find() because you're comparing a year string to a year number .注意:您不能在find()的回调中使用严格比较 ( === ),因为您将年份字符串与年份数字进行比较。 Probably a good idea to address that.解决这个问题可能是个好主意。

Because that return statement is inside of the handler of the function forEach , basically, you're returning the handler execution and not the main function.因为该return语句位于函数forEach的处理程序内,所以基本上,您返回的是处理程序执行而不是主函数。

What you need to do is either to use a for-loop or the function find as follow:您需要做的是使用 for 循环或函数find如下:

let found = items.find(d => d.year === year);
if (found) return found.price;

Or a vanilla for-loop:或香草 for 循环:

for (let i = 0; i < items.length; i++) 
   if (items[i].year === year) return items[i].price;

This needs ES6 (babel).这需要 ES6 (babel)。 Hope this helps!希望这可以帮助! Got it from https://zellwk.com/blog/looping-through-js-objects/ .https://zellwk.com/blog/looping-through-js-objects/得到它。

 const year = 1910; const items = [ { name: 'gallon of gas', year: 1910, price: .12 }, { name: 'gallon of gas', year: 1960, price: .30 }, { name: 'gallon of gas', year: 2010, price: 2.80 } ] const values = Object.values(items) for (const value of values) { //you can't return, but you can use the value or store it in a variable/array console.log(value.price); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用扩展运算符创建要在forEach循环中添加到JavaScript对象的动态键 - How do I create a dynamic key to be added to a JavaScript object in forEach loop using spread operator 如何从Angular中的foreach循环返回数组数据 - How do I return array data from a foreach loop in Angular 如何在foreach循环中仅显示对象一次? - How do i show object only once in foreach loop? 如何在 forEach 循环中获取对象的索引? - How do I get the index of an object in a forEach loop? 如何在 javascript nightwatch.js 测试自动化脚本中使用 forEach 循环将键值对分配给 object 数组? - How do I assign a key value pair to an object array with a forEach loop in javascript nightwatch.js test automation script? 如何使用 forEach 循环在 IIFE 中调用数组? - How do I call an array within an IIFE using a forEach loop? 如果我想使用 forEach 循环显示数组中的前 5 个元素怎么办? - What to do if i want to show the first 5 elements from the array by using forEach loop? 我有数组数组,我想要对象数组作为回报,我将如何在 JS 中或使用下划线插件执行此操作 - I have array of array, and i want array of object in return how would I do this in JS or using underscore-plugin 如何反转EJS forEach循环 - How do I reverse a EJS forEach loop 如何从数组中删除所需的对象,然后使用.forEach将它们添加到另一个数组中? - How do I remove the objects I want from an array and add them to another array using .forEach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM