使用forEach映射对象数组的Object.entries返回未定义,除非使用console.log [英] Object.entries with forEach to map array of objects returns undefined except when console.log is used

查看:64
本文介绍了使用forEach映射对象数组的Object.entries返回未定义,除非使用console.log的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有键值对的对象数组,我想在每个对象中找到一个特定的键并返回值.

数组的示例:

  filterCounties = [{"StateName":特拉华州","CountyName":肯特","FIPS":10001,"Eligibles_2017":33292,"Eligibles_2018":34938,"Penetration_2017":0.107,"Penetration_2018":0.123},{"StateName":特拉华州","CountyName":新城堡","FIPS":10003,"Eligibles_2017":94030,"Eligibles_2018":98080,"Penetration_2017":0.128,"Penetration_2018":0.164},{"StateName":特拉华州","CountyName":"Sussex","FIPS":10005,"Eligibles_2017":61964,"Eligibles_2018":65990,"Penetration_2017":0.082,渗透率_2018":0.097},{"StateName":特拉华州","CountyName":待定县名称","FIPS":10,"Eligibles_2017":9"Eligibles_2018":0,"Penetration_2017":0,"Penetration_2018":0}] 

我正在寻找的关键是Penetration_2018-我可以使用以下代码并使用console.log输出值

  const mapTest = new Map(Object.entries(filterCounties).forEach(([[key,value])=>console.log((key,value ["Penetration_2018"])))); 

这将输出值: console.log输出

但是,如果我先分配一个变量然后记录该变量

  const mapTest = new Map(Object.entries(filterCounties).forEach(([键,值])=>(键,值["Penetration_2018"])));console.log(`这是mapTest $ {mapTest}`); 

我得到的[object Map]没有值-我认为使用箭头功能时,返回是隐式的吗?

输出图像-带有或不带有字符串串联在此处输入图像描述

我最终想要提取这些值,以将它们分配给Victory BarChart的Y轴.感谢您的任何见解/指导.

解决方案

.forEach 不返回值或数组.尝试改用 .map .另外,在这种情况下,您不需要Java语言的 Map (它不能有重复的键),而且还要考虑到Victory BarChart似乎将 Array 作为数据(不是 Map ).

这是我建议的更改:

  const mapTest = filterCounties.map(元素=>({'Penetration_2018':element ['Penetration_2018']})) 

I have an array of objects that have key value pairs, I want to find a specific key in each object and return the value.

A sample of the Array:

filterCounties=[
  {
    "StateName": "Delaware",
    "CountyName": "Kent",
    "FIPS": 10001,
    "Eligibles_2017": 33292,
    "Eligibles_2018": 34938,
    "Penetration_2017": 0.107,
    "Penetration_2018": 0.123
  },
  {
    "StateName": "Delaware",
    "CountyName": "New Castle",
    "FIPS": 10003,
    "Eligibles_2017": 94030,
    "Eligibles_2018": 98080,
    "Penetration_2017": 0.128,
    "Penetration_2018": 0.164
  },
  {
    "StateName": "Delaware",
    "CountyName": "Sussex",
    "FIPS": 10005,
    "Eligibles_2017": 61964,
    "Eligibles_2018": 65990,
    "Penetration_2017": 0.082,
    "Penetration_2018": 0.097
  },
  {
    "StateName": "Delaware",
    "CountyName": "Pending County Designation",
    "FIPS": 10,
    "Eligibles_2017": 9,
    "Eligibles_2018": 0,
    "Penetration_2017": 0,
    "Penetration_2018": 0
  }
]

The key I am looking for is Penetration_2018 - I can use the following code and output the values using console.log

const mapTest = new Map(
      Object.entries(filterCounties).forEach(([key, value]) =>
        console.log((key, value["Penetration_2018"]))
      )
    );

This will output the values: console.log output

However if I assign to a variable and then log the variable

 const mapTest = new Map(
      Object.entries(filterCounties).forEach(
        ([key, value]) => (key, value["Penetration_2018"])
      )
    );
    console.log(`This is mapTest ${mapTest}`);

I get [object Map] with no values - I thought that when using an arrow function, return was implicit?

Image of output - with or without String concatenation enter image description here

I ultimately want to extract these values to assign them to the Y axis of a Victory BarChart. Thanks for any insight / guidance.

解决方案

.forEach does not return a value or an array. Try using .map instead. Also, you don't need a Javascript Map in this case (it can't have repeated keys), considering furthermore that Victory BarChart seems to take an Array as data (not a Map).

This is my suggested change:

const mapTest = filterCounties.map(
  element => ({'Penetration_2018': element['Penetration_2018']})
)

这篇关于使用forEach映射对象数组的Object.entries返回未定义,除非使用console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆