Jelajahi Sumber

08 need 3 more

pocu46 4 tahun lalu
induk
melakukan
945c41b02a
2 mengubah file dengan 104 tambahan dan 0 penghapusan
  1. 11 0
      08/index.html
  2. 93 0
      08/script.js

+ 11 - 0
08/index.html

@@ -0,0 +1,11 @@
+<!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 src="./script.js"></script>
+</body>
+</html>

+ 93 - 0
08/script.js

@@ -0,0 +1,93 @@
+//-----------------------------------------------------------sort---------------------------------------------------------------------------
+
+var persons = [{
+        name: "Иван",
+        age: 17
+    },
+    {
+        name: "Мария",
+        age: 35
+    },
+    {
+        name: "Алексей",
+        age: 73
+    },
+    {
+        name: "Яков",
+        age: 12
+    },
+]
+
+function sort(array, key, increase = true) {
+    if (increase) {
+        array.sort(function (a, b) {
+            if (a[key] > b[key]) {
+                return 1;
+            }
+            if (a[key] < b[key]) {
+                return -1;
+            }
+            return 0;
+        });
+    } else {
+        array.sort(function (a, b) {
+            if (a[key] < b[key]) {
+                return 1;
+            }
+            if (a[key] > b[key]) {
+                return -1;
+            }
+            return 0;
+        });
+    }
+    return array;
+};
+
+// console.log(sort(persons, "age")); //сортирует по возрасту по возрастанию
+// console.log(sort(persons, "name"));
+// console.log(sort(persons, "age", true));
+// console.log(sort(persons, "name", true));
+// console.log(sort(persons, "name", false)); //сортирует по имени по убыванию
+// console.log(sort(persons, "age", false)); //сортирует по имени по убыванию
+
+//-----------------------------------------------------------array map----------------------------------------------------------------------
+
+let arrayMap = ["1", {}, null, undefined, "500", 700];
+
+let arrayMap2 = arrayMap.map((item) => {
+    if (typeof item == "string") return isNaN(parseInt(item, 10)) ? item : parseInt(item, 10);
+    return item;
+});
+
+// console.log(arrayMap2);
+
+//-----------------------------------------------------------array reduce--------------------------------------------------------------------
+
+let arrayReduce = ["0", 5, 3, "string", null];
+
+let arrayPow = arrayReduce.reduce((a, b) => (typeof b == "number" ? a * b : a), 1);
+
+console.log(arrayPow);
+
+//-----------------------------------------------------------object filter--------------------------------------------------------------------
+
+var phone = {
+    brand: "meizu",
+    model: "m2",
+    ram: 2,
+    color: "black",
+};
+
+function filter(obj) {
+    let obj2 = {};
+    for (let key in obj) {
+        if (key == "color" || obj[key] == 2) {
+            obj2[key] = obj[key];
+        }
+    }
+    return obj2;
+}
+
+console.log(filter(phone));
+
+//-----------------------------------------------------------object map--------------------------------------------------------------------