[英]Extract specific value from a property from a map
I currently have the following data: 我目前有以下数据:
const data = [
{
Zone: 'Airport',
Lane: 'AIRDEL',
Capacity: '90',
Status: '80',
Spaces: '10',
LastAuditedOn: 'due',
AuditedBy: 'Ross',
FirstCollection: '-',
LastCollection: '-',
},
{
Zone: 'Arrivals',
Lane: '4',
Capacity: '10',
Status: '0',
Spaces: '10',
LastAuditedOn: '-',
AuditedBy: '-',
FirstCollection: '9:00PM',
LastCollection: '01:00AM',
},
{
Zone: 'Unknown',
Lane: 'BHX_HOLD1',
Capacity: '80',
Status: '40',
Spaces: '40',
LastAuditedOn: 'due',
AuditedBy: 'Max',
FirstCollection: '-',
LastCollection: '-',
},
]
and I am currently trying to extract the data based off the properties (zone, lane, capacity) as an example. 我目前正在尝试基于属性(区域,车道,通行能力)提取数据作为示例。
const multiHeaderBy = 'Zone';
data.forEach((key) => {
console.log(key.find(multiHeaderBy))
});
I expect values arrivals, airport, unknown
to appear in the console.log
but i get undefined. 我希望值
arrivals, airport, unknown
值出现在console.log
但是我不确定。
Ideas? 想法?
You can do this simply using .map
and .join()
methods like: 您可以简单地使用
.map
和.join()
方法执行此操作,例如:
const data=[{Zone:'Airport',Lane:'AIRDEL',Capacity:'90',Status:'80',Spaces:'10',LastAuditedOn:'due',AuditedBy:'Ross',FirstCollection:'-',LastCollection:'-',},{Zone:'Arrivals',Lane:'4',Capacity:'10',Status:'0',Spaces:'10',LastAuditedOn:'-',AuditedBy:'-',FirstCollection:'9:00PM',LastCollection:'01:00AM',},{Zone:'Unknown',Lane:'BHX_HOLD1',Capacity:'80',Status:'40',Spaces:'40',LastAuditedOn:'due',AuditedBy:'Max',FirstCollection:'-',LastCollection:'-',}]; const multiHeaderBy = 'Zone'; var txt = data.map(x => x[multiHeaderBy]); console.log(txt.join(', '));
Use map function to get array using key 使用map函数使用键获取数组
var valueArr = data.map(e=>e.Zone);
console.log(valueArr) // ["Airport", "Arrivals", "Unknown"]
You have to use [] backet instead of find function. 您必须使用[] backet而不是find函数。 [] backet is used to get the value from an object using dynamic key.
[] backet用于使用动态键从对象获取值。
const multiHeaderBy = 'Zone';
data.forEach((key) => {
console.log(key[multiHeaderBy])
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.