How Do I Find Unique Elements in an Array?
const tsv = [
"Location\tContainer\tBox\tItem\tQty\tValue",
"Rack\tBin 1\tClamps\tSmall\tClamps\t4\t0",
"Rack\tBin 1\tClamps\tMed\tClamps\t4\t0",
"Rack\tBin 1\tClamps\tCorner\tClamp\t1\t0",
"Rack\tBin 1\tElectrical\t3\tPole\tWall\tSockets\t15A\t8\t0",
"Rack\tBin 1\tElectrical\tSocket/Switch\tCombo\t1\t0",
"Rack\tBin 1\tElectrical\tLutron\tMS-OPS5M\tLight\tSensor\t1\t10",
"Rack\tBin 1\tElectrical\t100ft\t-\t14Ga\tElec.\tWire\t3\t0",
"Rack\tBin 1\tElectrical\t2\tGang\tElec.\tBox\t\t1\t0",
"Rack\tBin 1\tElectrical\t2\tGang\tElec.\tBox\tcovers\t3\t0"
];
const lines = tsv.slice(1);
const inventory = [];
lines.forEach((line, id) => {
const row = line.split("\t");
const [loc, container, box, item, qty, worth] = row;
inventory.push({ loc, container, box, item, qty, worth });
});
const boxes = inventory.reduce((boxes, obj) => {
if (!boxes.includes(obj.box)) {
boxes.push(obj.box);
}
return boxes;
}, []);
console.log(`The inventory contains these unique boxes\n\n\t${boxes.join(",")}`);
Output:
The inventory contains these unique boxes
Clamps,Electrical