In: Computer Science
(a) Define a class for your item including a constructor.
(b) Write a function that loads csv text(the function up to you) then creates an array of items using the above class’s constructor.
(c) Add a print method to your class. Write test code that prints your item array using this method.
Your faceted search item will have at least one categorical attribute. For example a clothing store item could have an attribute called colour, its values could be red, white, black etc.
Your faceted search item will have at least one categorical attribute. For example a clothing store item could have an attribute called colour, its values could be red, white, black etc. For the next question choose one of your categorical attributes.
use javascript
class Item {
constructor(name, color) {
this.name = name;
this.color = color;
}
print(){
console.log("Name: ",this.name, "\nColor: ",this.color);
}
}
function loadcsv(filename,delimiter=","){
var fs = require('fs');
var array = fs.readFileSync(filename).toString().split("\n");
var items = new Array();
array.forEach(element => {
obj = element.split(delimiter);
items.push(new Item(obj[0],obj[1]));
});
return items;
}
items = loadcsv("./list.csv")
console.log(items);
Here is the snapshot of run.