我目前有以下数据:
code . \n
我正在尝试根据属性(区域,通道,容量)提取数据作为示例。
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';
data.forEach((key) => {
console.log(key.find(multiHeaderBy))
});
出现在arrivals, airport, unknown
中,但我未定义。
想法?
答案 0 :(得分:1)
您只需使用.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(', '));

答案 1 :(得分:0)
使用map函数使用键
获取数组var valueArr = data.map(e=>e.Zone);
console.log(valueArr) // ["Airport", "Arrivals", "Unknown"]
答案 2 :(得分:0)
你必须使用[] backet而不是find函数。 [] backet用于使用动态密钥从对象获取值。
const multiHeaderBy = 'Zone';
data.forEach((key) => {
console.log(key[multiHeaderBy])
});