123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- function arrSort() {
- let persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- function personSort() {
- persons.sort(function(a, b){
- return b.age - a.age
- })
- persons.sort(function(a, b){
- let nameA = a.name.toLowerCase();
- let nameB = b.name.toLowerCase();
- if (nameA > nameB)
- return -1
- if (nameA < nameB)
- return 1
- })
- }
- }
- //=====================================================================================================================//
- //=============================================== array map ===========================================================//
- let qwerty = ["1", {}, null, undefined, "500", 700].map((qwerty) => parseInt(qwerty) ? parseInt(qwerty) : qwerty);
- //=====================================================================================================================//
- //=============================================== array reduce ========================================================//
- let myArr = ["0", 5, 3, "string", null].reduce((a,myArr) => (!isNaN(myArr) && typeof myArr === "number" ? a * myArr : a), 1)
- //=====================================================================================================================//
- //=============================================== object filter =======================================================//
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function filter(arr, func) {
- for (let k in arr) {
- if (!func(k, arr[k])) {
- delete arr[k]
- }
- }
- return arr;
- }
- console.log(filter(phone, (key, value) => key == "color" || value == 2));
- //=====================================================================================================================//
- //=============================================== object map ==========================================================//
- const map = function (obj, func) {
- let objNew = {};
- for (let key in obj) {
- objNew = { ...objNew, ...func(key, obj[key]) };
- }
- return objNew;
- };
- let testObj = map({ name: "Иван", age: 17 }, function (key, value) {
- var result = {};
- result[key + "_"] = value + "$";
- return result;
- });
- </script>
- </body>
- </html>
|