123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- //-----------------------------------------------------------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--------------------------------------------------------------------
|