JS Snippets: Array of Objects: Difference between revisions

From WikiMLT
Line 5: Line 5:
*[https://github.com/metalevel-tech/exc-js-homework MLT GitHub: JavaScript Homework Tasks]
*[https://github.com/metalevel-tech/exc-js-homework MLT GitHub: JavaScript Homework Tasks]


== Data file of the examples ==
== Example data files ==
<syntaxhighlight lang="shell" class="code-continue">
<syntaxhighlight lang="shell" class="code-continue">
data.mjs
product-data.mjs
</syntaxhighlight><syntaxhighlight lang="javascript">
</syntaxhighlight><syntaxhighlight lang="javascript">
export default [
export default [
Line 13: Line 13:
     { product: "#2", type: "savory", price: 2.55 },
     { product: "#2", type: "savory", price: 2.55 },
     { product: "#3", type: "savory", price: 3.79 }
     { product: "#3", type: "savory", price: 3.79 }
];
</syntaxhighlight><syntaxhighlight lang="shell" class="code-continue">
podcast-data.mjs
</syntaxhighlight><syntaxhighlight lang="javascript">
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"],
    }
];
];
</syntaxhighlight>
</syntaxhighlight>
Line 18: Line 41:
== Sort Objects by a Property (Price) ==
== Sort Objects by a Property (Price) ==
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue">
import products from "./data.mjs";
import products from "./product-data.mjs";


function sortProducts(data: { product: string, type: string, price: number }[]) {
function sortProducts(data: { product: string, type: string, price: number }[]) {
Line 27: Line 50:
== Reduce the prices into a Sum ==
== Reduce the prices into a Sum ==
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue">
import products from "./data.mjs";
import products from "./product-data.mjs";


function sumPrices(data: { product: string, type: string, price: number }[]) {
function sumPrices(data: { product: string, type: string, price: number }[]) {
Line 38: Line 61:
== Reduce the prices of certain product type into a Sum ==
== Reduce the prices of certain product type into a Sum ==
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue">
import products from "./data.mjs";
import products from "./product-data.mjs";


function sumPrices(data: { product: string, type: string, price: number }[]) {
function sumPrices(data: { product: string, type: string, price: number }[]) {
Line 51: Line 74:
== Filter the Objects by a Property and Map a new Array ==
== Filter the Objects by a Property and Map a new Array ==
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue">
import products from "./data.mjs";
import products from "./product-data.mjs";


function sumPrices(data: { product: string, type: string, price: number }[]) {
function sumPrices(data: { product: string, type: string, price: number }[]) {
Line 58: Line 81:
             .map(({ item, price }) => ({ item, price }));
             .map(({ item, price }) => ({ item, price }));
}
}
</syntaxhighlight><noinclude><div id='devStage'>
</syntaxhighlight>
 
== Get unique Tags from Podcast-data ==
<noinclude><div id='devStage'>
{{devStage  
{{devStage  
  | Прндл  = JavaScript
  | Прндл  = JavaScript

Revision as of 15:47, 11 March 2023

Ref­er­ences

Ex­am­ple da­ta 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 Ob­jects by a Prop­er­ty (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);
}

Re­duce the prices in­to 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);
}

Re­duce the prices of cer­tain prod­uct type in­to 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);
}

Fil­ter the Ob­jects by a Prop­er­ty and Map a new Ar­ray

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 Pod­cast-da­ta