123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //-----------------------------------------------------------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--------------------------------------------------------------------
- let someObject = {
- name: "Иван",
- age: 17
- };
- function map(obj) {
- let temp = {};
- for (let i in obj) {
- temp[i + '_'] = i + '$';
- }
- return temp;
- }
- console.log(map(someObject));
- //-----------------------------------------------------------recursion--------------------------------------------------------------------
- function arithmeticProgression(num, increment, rows) {
- if (rows <= 1) {
- return num;
- }
- return num + arithmeticProgression(num + increment, increment, rows - 1)
- }
- console.log(arithmeticProgression(1, 4, 8));
- //-----------------------------------------------------------HTML Tree--------------------------------------------------------------------
- var someTree = {
- tagName: "table", //html tag
- children: [ //вложенные тэги
- {
- tagName: "tr",
- children: [{
- tagName: "td",
- text: "some text",
- },
- {
- tagName: "td",
- text: "some text 2",
- }
- ]
- }
- ],
- attrs: {
- border: 1,
- },
- }
- function construct(obj) {
- let node = document.createElement(obj.tagName);
- if ("attrs" in obj) {
- for (let attrName in obj.attrs) {
- node.setAttribute(attrName, obj.attrs[attrName])
- }
- }
- if ("children" in obj) {
- for (let child in obj.children) {
- node.append(construct(obj.children[child]));
- }
- }
- if ("text" in obj) {
- node.innerText = obj.text;
- }
- return node;
- }
- document.body.append(construct(someTree));
- console.log(construct(someTree));
- if () {
- if () {
- dbvbskdnvn
- }
- }
|