|
@@ -1,82 +1,63 @@
|
|
// sort
|
|
// sort
|
|
-// var persons = [
|
|
|
|
-// {name: "Иван", age: 17},
|
|
|
|
-// {name: "Мария", age: 35},
|
|
|
|
-// {name: "Алексей", age: 73},
|
|
|
|
-// {name: "Яков", age: 12},
|
|
|
|
-// ]
|
|
|
|
-
|
|
|
|
-// persons.sort((a,b) => a.age > b.age ? 1 : -1)
|
|
|
|
-
|
|
|
|
-// // var sortBoolean = confirm('Сортировать по возрастанию или по убыванию?\n Если "ОК" - сортировака по возрастанию\n Если "ОТМЕНА" - сортировака по убыванию');
|
|
|
|
-// // if(sortBoolean === true){
|
|
|
|
-// // persons.sort((a,b) => a.name > b.name ? 1 : -1)
|
|
|
|
-// // }
|
|
|
|
-// // else persons.sort((a,b) => a.name > b.name ? -1 : 1)
|
|
|
|
|
|
+// const persons = [
|
|
|
|
+// { name: "Иван", age: 17 },
|
|
|
|
+// { name: "Мария", age: 35 },
|
|
|
|
+// { name: "Алексей", age: 73 },
|
|
|
|
+// { name: "Яков", age: 12 },
|
|
|
|
+// ];
|
|
|
|
+// sort = (arr, key, bool = true) => {
|
|
|
|
+// bool === false
|
|
|
|
+// ? arr.sort((a, b) => (a[key] > b[key] ? -1 : 1))
|
|
|
|
+// : arr.sort((a, b) => (a[key] > b[key] ? 1 : -1));
|
|
|
|
+// console.log(arr);
|
|
|
|
+// };
|
|
|
|
|
|
-// console.log(persons.sort());
|
|
|
|
|
|
+// sort(persons, "age"); //сортирует по возрасту по возрастанию
|
|
|
|
+// sort(persons, "name", false); //сортирует по имени по убыванию
|
|
|
|
|
|
// array map
|
|
// array map
|
|
-// var arrMay = ["1", {}, null, undefined, "500", 700]
|
|
|
|
-
|
|
|
|
-// var result = arrMay.map(function(item){
|
|
|
|
-// var number = parseInt(item);
|
|
|
|
-// return isNaN(number) ? item : number
|
|
|
|
-// })
|
|
|
|
-// console.log(result)
|
|
|
|
|
|
+// const arr = ["1", {}, null, undefined, "500", 700];
|
|
|
|
+// const arrToNum = (item) => (typeof item === "string" ? +item : item);
|
|
|
|
+// console.log(arr.map(arrToNum));
|
|
|
|
|
|
// array reduce
|
|
// array reduce
|
|
-// var arr = ["0", 5, 3, "string", null];
|
|
|
|
-
|
|
|
|
-// var result = arr.reduce(function(item, mul){
|
|
|
|
-// return typeof(mul) === 'number' ? item *= mul : item
|
|
|
|
-// }, 1)
|
|
|
|
-// console.log(result);
|
|
|
|
|
|
+// const arr = ["0", 5, 3, "string", null];
|
|
|
|
+// const result = (item, mul) => (typeof mul === "number" ? (item *= mul) : item);
|
|
|
|
+// console.log(arr.reduce(result, 1));
|
|
|
|
|
|
// object filter
|
|
// object filter
|
|
-// var phone = {
|
|
|
|
|
|
+// const phone = {
|
|
// brand: "meizu",
|
|
// brand: "meizu",
|
|
// model: "m2",
|
|
// model: "m2",
|
|
// ram: 2,
|
|
// ram: 2,
|
|
// color: "black",
|
|
// color: "black",
|
|
// };
|
|
// };
|
|
-
|
|
|
|
-// function filter(obj, key, value) {
|
|
|
|
-// var itog = {};
|
|
|
|
-// for (let item in obj) {
|
|
|
|
-// if (key === item || obj[item] === value) itog[item] = obj[item];
|
|
|
|
|
|
+// filter = (obj, fun) => {
|
|
|
|
+// let obj1 = {};
|
|
|
|
+// for (const key in obj) {
|
|
|
|
+// if (fun(key, obj[key])) {
|
|
|
|
+// obj1[key] = obj[key];
|
|
|
|
+// }
|
|
// }
|
|
// }
|
|
-// return itog;
|
|
|
|
-// }
|
|
|
|
-// console.log(filter(phone, "color", 2))
|
|
|
|
-
|
|
|
|
|
|
+// console.log(obj1);
|
|
|
|
+// };
|
|
|
|
|
|
|
|
+// filter(phone, (key, value) => key == "color" || value == 2);
|
|
|
|
|
|
// object map
|
|
// object map
|
|
|
|
+// map = (obj, fun) => {
|
|
|
|
+// let obj1 = {};
|
|
|
|
+// for (const key in obj) {
|
|
|
|
+// let simb = fun(key, obj[key]);
|
|
|
|
+// for (const key2 in simb) {
|
|
|
|
+// obj1[key2] = simb[key2];
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// console.log(obj1);
|
|
|
|
+// };
|
|
|
|
+
|
|
// map({ name: "Иван", age: 17 }, function (key, value) {
|
|
// map({ name: "Иван", age: 17 }, function (key, value) {
|
|
-// var result = {};
|
|
|
|
|
|
+// const result = {};
|
|
// result[key + "_"] = value + "$";
|
|
// result[key + "_"] = value + "$";
|
|
// return result;
|
|
// return result;
|
|
-// });
|
|
|
|
-//должен вернуть {name_: "Иван$", age_: "17$"}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-// var person = [
|
|
|
|
-// {name: "Иван"},
|
|
|
|
-// {age: 17}
|
|
|
|
-// ]
|
|
|
|
-
|
|
|
|
-// var newPerson = person.map(function(obj){
|
|
|
|
-// var result = {}
|
|
|
|
-
|
|
|
|
-// for (var key in obj){
|
|
|
|
-// var personName = key + "_"
|
|
|
|
-// var personAge = obj[key] + "$"
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// result[personName] = personAge
|
|
|
|
-// return result
|
|
|
|
-// })
|
|
|
|
-
|
|
|
|
-// console.log(newPerson)
|
|
|
|
|
|
+// });
|