123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- let persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- function sort(field, boolean = true) {
- if (boolean) {
- return (a, b) => a[field] > b[field] ? 1 : -1
- }else {
- return (a, b) => a[field] < b[field] ? 1 : -1
- }
- }
- console.log(persons.sort(sort('age', false)))
- let arr = ["1", {}, null, undefined, "500", 700];
- let newArr = arr.map(function(a){
- return typeof(a) === 'string' ? parseInt(a) : a;
- });
- console.log(arr);
- console.log(newArr);
- var arr = ["0", 5, 3, "string", null]
- var result = arr.reduce(function(previousValue, currentValue){
- if(typeof(currentValue)==='number'){
- previousValue*=currentValue;
- };
- return previousValue
- }, 1)
- alert(result);
-
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function filter(object, someF) {
- for(let key in object){
- if(!someF(key, object[key])) {
- delete object[key]
- }
- }
- }
- filter(phone,(key,value) => key == "color" || value == 2);
- console.log(phone)
- let obj = map({name: "Иван", age: 17},function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- return result;
- })
- function map (object , callback){
- let obj = {}
- for(let key in object) {
- Object.assign(obj,callback(key, object[key]));
- }
- return obj
- }
- console.log(obj);
- function sumTo(n) {
- if (n == 1) return 1;
- return n + sumTo(n - 1);
- }
-
- alert( sumTo(10) );
-
|