I required to extract unique keys from array, leaving unwanted keys. here is my try it works fine. But looking for the easiest way.
const data = [{
"year": "2021",
"europe": 5,
"namerica": 2.5,
"asia": 1
}, {
"year": "2022",
"europe": 2.6,
"namerica": 6.7,
"asia": 2.2
}, {
"year": "2023",
"europe": 4.8,
"namerica": 1.9,
"asia": 4.4
}];
const uniqueLables = [];
const omit = ["year"]
for (let i = 0; i < data.length; i++) {
for (const [key, value] of Object.entries(data[i])) {
if (!uniqueLables.includes(key)) {
if (!omit.includes(key)) {
uniqueLables.push(key);
}
}
}
}
console.log(uniqueLables) // => ['europe', 'namerica', 'asia']