drunk_ppl = [
{name: 'Mark', age: 20, alcohol_type: 'Whiskey'},
{name: 'Jon', age: 25, alcohol_type: 'Rum'},
{name: 'April', age: 22, alcohol_type: 'Gin'},
{name: 'Simon', age: 50, alcohol_type: 'Vodka'}
];
smoker = [
{name: 'Mark', cig_a_day: 20, brand: 'Marlboro'},
{name: 'Jon', cig_a_day: 15, brand: 'Camel'},
{name: 'April', cig_a_day: 12, brand: 'Newport'},
{name: 'Simon', cig_a_day: 5, brand: 'Dunhill'}
];
var drunkard = []; //creating empty array
for(var i = 0; i < drunk_ppl.length; i++) {
drunkard[i] = drunk_ppl[i].name;
}
需要有关此代码段的帮助。
基本上我想要这样的东西:
drunk_ppl
中的人
在drunkard
drunkard
数组的取一个名称并使用该名称从smoker
数组中查找cigarrette的品牌。
如果在吸烟者阵列中找不到匹配项,则不执行任何操作
答案 0 :(得分:3)
检查这个片段,你可以将它包装在一个函数上,你很高兴
smoker.filter(x=>x.name==="Mark").forEach(x=>console.log(x.brand));
答案 1 :(得分:1)
drunk_ppl.map(ele=>ele.name). // return array of name from drunk_ppl
forEach(function(ele, index){ //loop through array of names
console.log(smoker.find(element=> element.name === ele).brand) /*find brands for name*/
})