// A record-like factory function
function Item(a, b, label) {
return Object.freeze({ a, b, label });
}
// Create dataset
function makeData() {
return [
Item(7, 2, "python"),
Item(8, 3, "c"),
Item(3, 5, "c++"),
Item(4, 1, "c#"),
Item(3, 2, "java"),
Item(7, 1, "go"),
Item(1, 2, "rust")
];
}
// Sort by a, then b
function sortData(data) {
data.sort((x, y) =>
x.a !== y.a ? x.a - y.a : x.b - y.b
);
}
// Print all items
function printData(data) {
for (const item of data) {
console.log(`(${item.a}, ${item.b}, ${item.label})`);
}
}
// Find first item by label
function findByLabel(data, label) {
return data.find(item => item.label === label) || null;
}
// Filter by first field
function filterByA(data, value) {
return data.filter(item => item.a === value);
}
// Main program
function main() {
const data = makeData();
sortData(data);
console.log("Sorted data:");
printData(data);
console.log("\nSearching for 'java':");
const found = findByLabel(data, "java");
if (found) console.log(found);
console.log("\nFiltering items where a == 7:");
const filtered = filterByA(data, 7);
printData(filtered);
}
main();
/*
run:
Sorted data:
(1, 2, rust)
(3, 2, java)
(3, 5, c++)
(4, 1, c#)
(7, 1, go)
(7, 2, python)
(8, 3, c)
Searching for 'java':
{ a: 3, b: 2, label: 'java' }
Filtering items where a == 7:
(7, 1, go)
(7, 2, python)
*/