123456789101112131415161718192021222324252627282930313233343536373839 |
- <header>
- <h1>ArreyOfObject</h1>
- </header>
- <body>
- <script>
- const toHtmlTable = (arr) => {
- let str = "<table>";
- for (let arrEl of arr) {
- str += "<tr>";
- for (let val of arrEl) {
- str += `<td>${val}</td>`;
- }
- str += "</tr>";
- }
- str += "</table>";
- return str;
- }
- var persons = [
- { name: "Иван", age: 17 },
- { name: "Мария", age: 35 },
- { name: "Алексей", age: 73 },
- { name: "Яков", age: 12 },
- ]
- /*a b comp< asc res
- 1 2 true false false
- 2 1 false false true
-
- 1 2 true true true
- 2 1 false true false*/
- const sort = (arr, name, asc = true) =>
- arr.sort((a, b) => (a[name] < b[name]) == asc ? -1 : 1);
- sort(persons, "age"); //сортирует по возрасту по возрастанию
- document.write(toHtmlTable(persons.map(p => Object.entries(p))));
- sort(persons, "name", false); //сортирует по имени по убыванию
- document.write(toHtmlTable(persons.map(p => Object.entries(p))));
- </script>
- </body>
|