|
@@ -0,0 +1,103 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+
|
|
|
+<body>
|
|
|
+ <script>
|
|
|
+ //sort
|
|
|
+ var persons = [
|
|
|
+ { name: "Иван", age: 17 },
|
|
|
+ { name: "Мария", age: 35 },
|
|
|
+ { name: "Алексей", age: 73 },
|
|
|
+ { name: "Яков", age: 12 },
|
|
|
+ ]
|
|
|
+
|
|
|
+ function sort(arr, key, boolean = true) {
|
|
|
+ arr.sort(bool)
|
|
|
+ function bool(a, b) {
|
|
|
+ if (boolean) {
|
|
|
+ return (a[key] > b[key]) ? 1 : -1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return (a[key] < b[key]) ? 1 : -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ sort(persons, "name")
|
|
|
+
|
|
|
+
|
|
|
+ //array map
|
|
|
+ let array = ["1", {}, null, undefined, "500", 700]
|
|
|
+ let y = array.map(x => {
|
|
|
+ let number = Number(x)
|
|
|
+ return isNaN(x) ? x : number
|
|
|
+ })
|
|
|
+ console.log(y)
|
|
|
+
|
|
|
+ //array reduce
|
|
|
+ let array = ["0", 5, 3, "string", null]
|
|
|
+ let result = array.reduce((a, b) =>
|
|
|
+ typeof (b) === "number" ? a * b : a, 1
|
|
|
+ )
|
|
|
+ console.log(result);
|
|
|
+
|
|
|
+ //object filter
|
|
|
+ var phone = {
|
|
|
+ brand: "meizu",
|
|
|
+ model: "m2",
|
|
|
+ ram: 2,
|
|
|
+ color: "black",
|
|
|
+ };
|
|
|
+ filter(phone, (key, value) => key == "color" || value == 2);
|
|
|
+
|
|
|
+ function filter(obj, key, value) {
|
|
|
+ let newObj = {}
|
|
|
+ for (let key in obj) {
|
|
|
+ for (let value in obj) {
|
|
|
+ if (obj[key] == 2 && value == "color") {
|
|
|
+ newObj[key] = obj[key]
|
|
|
+ newObj[value] = obj[value]
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newObj
|
|
|
+ }
|
|
|
+
|
|
|
+ //object map
|
|
|
+ function map(obj, callback) {
|
|
|
+ let result1 = {};
|
|
|
+ for (let key in obj) {
|
|
|
+ result1 = { ...result1, ...callback(key, obj[key]) }
|
|
|
+ }
|
|
|
+ return result1
|
|
|
+ }
|
|
|
+
|
|
|
+ let miniObj = map({ name: "Иван", age: 17 }, function (key, value) {
|
|
|
+ var result = {};
|
|
|
+ result[key + "_"] = value + "$";
|
|
|
+ return result;
|
|
|
+ })
|
|
|
+ console.log(miniObj)
|
|
|
+
|
|
|
+ //Sum
|
|
|
+ function sum (min, max, step){
|
|
|
+ if(max <= 0){
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return sum (min +step, max -1, step) + min
|
|
|
+ }
|
|
|
+ console.log(sum(1,5,3))
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|