123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- var persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- persons.sort((a,b) => a.age > b.age ? 1 : -1);
- sort(persons, "age");
- //
- persons.sort((a,b) => a.name > b.name ? 1 : -1);
- sort(persons, "name", false);
- //array map
- let arr = ["1", {}, null, undefined, "500", 700];
- let result = arr.map(function(item){
- let number = parseInt(item);
- return isNaN(number) ? item : number;
- });
- console.log(result);
- //array reduce
- let arr2 = ["0", 5, 3, "string", null];
- let arr3 = arr2.filter(item => typeof item === 'number');
- arr3.reduce((a, b) => a * b);
- console.log(number02)
- //object filter
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function filter (a, b){
- let phone = {}
- for (let key in a){
- if (b (key, a[key])) {
- phone [key] = a[key];
- }
- }
- return phone
- }
- console.log(filter(phone,(key,value) => key == "color" || value == 2));
- //object map
- function personeMap(a, b){
- let filter = {}
- for ( let key in a){
- filter = {...filter, ...b([key], a[key])};
- }
- return filter
- }
- let resultat = personeMap({name: "Иван", age: 17},function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- return result;
- })
- console.log(resultat)
|