JS Snippets: Array of Objects: Difference between revisions
From WikiMLT
Line 84: | Line 84: | ||
== Get unique Tags from Podcast-data == | == Get unique Tags from Podcast-data == | ||
<noinclude><div id='devStage'> | <syntaxhighlight lang="typescript" class="code-continue"> | ||
import products from "./podcast-data.mjs"; | |||
function getUniqueTagsForInLoop(data) { | |||
const unqTagsTrack = {}; | |||
const unqTagsList = []; | |||
data | |||
.map(({ tags }) => tags) | |||
.flat().forEach(tag => { | |||
unqTagsTrack[tag] = true; | |||
}); | |||
for (const key in unqTagsTrack) { | |||
unqTagsList.push(key); | |||
} | |||
return unqTagsList; | |||
} | |||
</syntaxhighlight><syntaxhighlight lang="typescript" class="code-continue"> | |||
import products from "./podcast-data.mjs"; | |||
function getUniqueTagsWithFilter(data) { | |||
const unqTagsTrack = {}; | |||
return data | |||
.map(({ tags }) => tags) | |||
.flat() | |||
.filter(tag => { | |||
if (!unqTagsTrack[tag]) { | |||
unqTagsTrack[tag] = true; | |||
return true; | |||
} | |||
return false; | |||
}); | |||
} | |||
</syntaxhighlight><noinclude><div id='devStage'> | |||
{{devStage | {{devStage | ||
| Прндл = JavaScript | | Прндл = JavaScript |
Revision as of 14:49, 11 March 2023
References
Example data files
product-data.mjs
export default [
{ product: "#1", type: "sweet", price: 7.54 },
{ product: "#2", type: "savory", price: 2.55 },
{ product: "#3", type: "savory", price: 3.79 }
];
podcast-data.mjs
export default [
{
id: 1,
title: "Stranger Scrims",
duration: 40,
tags: ["supernatural", "horror", "drama"],
},
{
id: 2,
title: "The Scrim of the Dragon",
duration: 60,
tags: ["drama", "fantasy"],
},
{
id: 3,
title: "Scrim Hunters",
duration: 22,
tags: ["reality", "home improvement"],
}
];
Sort Objects by a Property (Price)
import products from "./product-data.mjs";
function sortProducts(data: { product: string, type: string, price: number }[]) {
return data.sort((a, b) => a.price - b.price);
}
Reduce the prices into a Sum
import products from "./product-data.mjs";
function sumPrices(data: { product: string, type: string, price: number }[]) {
return parseFloat(
data.reduce((acc, { price }) => acc + price, 0)
).toFixed(2);
}
Reduce the prices of certain product type into a Sum
import products from "./product-data.mjs";
function sumPrices(data: { product: string, type: string, price: number }[]) {
return parseFloat(
data
.filter(({ type }) => type === "savory")
.reduce((acc, { price }) => acc + price, 0)
).toFixed(2);
}
Filter the Objects by a Property and Map a new Array
import products from "./product-data.mjs";
function sumPrices(data: { product: string, type: string, price: number }[]) {
return data
.filter(({ type }) => type === "savory")
.map(({ item, price }) => ({ item, price }));
}
Get unique Tags from Podcast-data
import products from "./podcast-data.mjs";
function getUniqueTagsForInLoop(data) {
const unqTagsTrack = {};
const unqTagsList = [];
data
.map(({ tags }) => tags)
.flat().forEach(tag => {
unqTagsTrack[tag] = true;
});
for (const key in unqTagsTrack) {
unqTagsList.push(key);
}
return unqTagsList;
}
import products from "./podcast-data.mjs";
function getUniqueTagsWithFilter(data) {
const unqTagsTrack = {};
return data
.map(({ tags }) => tags)
.flat()
.filter(tag => {
if (!unqTagsTrack[tag]) {
unqTagsTrack[tag] = true;
return true;
}
return false;
});
}