JS Snippets: Array of Objects: Difference between revisions
From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разработван]; Категория:JavaScript) |
m (→References) |
||
Line 1: | Line 1: | ||
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | <noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | ||
== References == | == References == | ||
* [https:// | *[https://github.com/metalevel-tech/exc-js-homework MLT GitHub: JavaScript Homework Tasks] | ||
== | == 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
References
Sort Objects by a Property
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);
}