123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /* Сортировка */
- var persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- function sort(array, field, order = true) {
- let arrSorted = array.sort(function (a, b){
- if (a[field] > b[field]) {
- return order ? 1 : -1;
- }
- else{
- return order ? -1 : 1;
- }
- })
- return arrSorted
- }
- sort(persons, 'age', false)
- /* array map */
- let arr = ["1", {}, null, undefined, "500", 700]
- let arr1 = arr.map(x => typeof x === 'string' ? +x : x)
- console.log(arr1)
- /* array reduce */
- let arr2 = ["0", 5, 3, "string", null];
- let arr3 = arr2.filter(x => typeof x === 'number').reduce((a, b) => a *= b)
- console.log(arr3)
- /* object filter */
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- filter(phone, (key, value) => key == "color" || value == 2);
- function filter(obj, validator) {
- let object = {};
-
- for (let key in obj) {
- if (validator(key, obj[key])) {
- object[key] = obj[key];
- }
- }
- return object;
- }
- /* object map */
- map({name: "Иван", age: 17},function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- return result;
- })
- function map(obj, validator) {
- let object = {}
- for (key in obj) {
- object = {...object, ...validator(key, obj[key])}
- }
- return object
- }
- /* Рекурсия: sum */
- function progression(n) {
- if (n === 0) {
- return 0
- } else {
- return n + progression(n - 1)
- }
- }
|