如何有选择地打印数组中的列表部分?

问题描述:

所以我有一个CSV满饮数据如何有选择地打印数组中的列表部分?

饮料,ABV,类型杜松子酒,45,精神普罗塞克,11,酒伏特加,40,精神 苦艾酒,70,精神雪利酒,20,葡萄酒烈性黑啤酒, 8,啤酒贮藏啤酒,4,啤酒 茴香烈酒,37,精神

这是我的JS,我想它要经过阵列和检查烈酒,然后打印出在控制台的精神。

var spirit_list = []; 

function drink(a,b,c) { 
    this.Drink = a; 
    this.ABV = b; 
    this.Type = c; 

} 

d3.csv("alcohol.csv", function(data) { 
    data.forEach(function(d){ 
     myDrink = new drink(); // new drink object 
     if (d.Type === "Spirit"){ //logic to grab spirits 
      myDrink.name = d.Drink; // assign obj values 
      myDrink.abv = +d.ABV; 
      spirit_list.push(myDrink) // push the obj to a list 
     }; 
    console.log(spirit_list); 
     // d.abv = +d.ABV; // + : converts into a number, instead of the default string 
    }) 
    // console.log(data); // data becomes sucked into console.log and becomes an array 
    // fyi everything is parsed into strings (from the spreadsheet .csv to the log) 
}); 

} 

但我只想名字和ABV展现出来,我不希望所有的其他对象属性(饮料,酒精度,和类型显示为未定义)

控制台日志输出:0:Object {Drink:undefined,name:“Gin”,abv:45,...}

可能吗?

+0

以及你将需要删除。如果该方法定义了a,b,c,那么当你调用它时,为什么不把它们传入? – epascarello

+0

这是我的助教建立的教程场景。我同意你的观点,但她不知何故(或者在她的屏幕截图中欺骗/遗漏了未定义的内容)删除了未定义的属性。我是否可以简单地通过为属性传递空字符串来修复它?但是,不管怎样,这个领域会显现出来吗? –

+0

比创建时不定义饮料。 – epascarello

你有你的drink函数,这很好,它是一个简单的'类',你可以用它来创建多个饮料列表。

function drink(a,b,c) { 
    this.Drink = a; 
    this.ABV = b; 
    this.Type = c; 

} 

你在drink定义的三个属性是当您创建的每个myDrink对象,你应该使用什么。像这样更改您的创建代码...在if语句中移动饮料创建,并只添加所需的属性。如果您有属性,你不需要在这种情况下使用,只需delete他们:

if (d.Type === "Spirit"){ //logic to grab spirits 
    myDrink = new drink(d.Drink, d.ABV, d.Type); // new drink object 
    delete myDrink['Type']; 
    spirit_list.push(myDrink) // push the obj to a list 
}; 

这里有一个完整的例子:

var spirit_list = []; 
 

 
function drink(a,b,c) { 
 
    this.Drink = a; 
 
    this.ABV = b; 
 
    this.Type = c; 
 

 
} 
 

 
getData().forEach(function(d){ 
 
    myDrink = new drink(); // new drink object 
 
    if (d.Type === "Spirit"){ //logic to grab spirits 
 
     myDrink = new drink(d.Drink, d.ABV, d.Type); 
 
     delete myDrink['Type']; 
 
     spirit_list.push(myDrink) // push the obj to a list 
 
    }; 
 
    // d.abv = +d.ABV; // + : converts into a number, instead of the default string 
 
}); 
 

 
console.log(spirit_list); 
 
// console.log(data); // data becomes sucked into console.log and becomes an array 
 
// fyi everything is parsed into strings (from the spreadsheet .csv to the log) 
 

 
function getData() { 
 
    return [ 
 
{ 
 
    "Drink": "Gin", 
 
    "ABV": 45, 
 
    "Type": "Spirit" 
 
}, 
 
{ 
 
    "Drink": "Prosecco", 
 
    "ABV": 11, 
 
    "Type": "Wine" 
 
}, 
 
{ 
 
    "Drink": "Vodka", 
 
    "ABV": 40, 
 
    "Type": "Spirit" 
 
}, 
 
{ 
 
    "Drink": "Absinthe", 
 
    "ABV": 70, 
 
    "Type": "Spirit" 
 
}, 
 
{ 
 
    "Drink": "Sherry", 
 
    "ABV": 20, 
 
    "Type": "Wine" 
 
}, 
 
{ 
 
    "Drink": "Stout", 
 
    "ABV": 8, 
 
    "Type": "Beer" 
 
}, 
 
{ 
 
    "Drink": "Lager", 
 
    "ABV": 4, 
 
    "Type": "Beer" 
 
}, 
 
{ 
 
    "Drink": "Ouzo", 
 
    "ABV": 37, 
 
    "Type": "Spirit" 
 
} 
 
]; 
 
}