JS Snippets: Array of Objects: Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разработван]; Категория:JavaScript)
Line 1: Line 1:
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude>
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude>
== References ==
== References ==


* [https://codewithmosh.com/p/object-oriented-programming-in-javascript Code with Mosh: The Ultimate JavaScript Mastery Series - Part 2]
*[https://github.com/metalevel-tech/exc-js-homework MLT GitHub: JavaScript Homework Tasks]
* [https://www.w3schools.com/js/default.asp W3School: JavaScript Tutorial]


== Section 1 ==
== Sort Objects by a Property ==
...
<syntaxhighlight lang="shell" class="code-continue">
data.mjs
</syntaxhighlight><syntaxhighlight lang="javascript">
export default [
    { product: "#1", price: 7.54 },
    { product: "#2", price: 2.55 },
    { product: "#3", price: 3.79 }
];
</syntaxhighlight><syntaxhighlight lang="shell" class="code-continue">
sort.ts
</syntaxhighlight><syntaxhighlight lang="typescript" class="code-continue">
import products from "./data.mjs";


<noinclude>
function sortProducts(data: { product: string, price: number }[]) {
    return data.sort((a, b) => a.price - b.price);
}
</syntaxhighlight><noinclude>
<div id='devStage'>
<div id='devStage'>
{{devStage  
{{devStage  

Revision as of 13:48, 11 March 2023

Ref­er­ences

Sort Ob­jects by a Prop­er­ty

data.mjs
export default [
    { product: "#1", price: 7.54 },
    { product: "#2", price: 2.55 },
    { product: "#3", price: 3.79 }
];
sort.ts
import products from "./data.mjs";

function sortProducts(data: { product: string, price: number }[]) {
    return data.sort((a, b) => a.price - b.price);
}