123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- // sort--------------------------------------------------------------------------------------------------------------------------;
- var persons = [
- { name: "Иван", age: 17 },
- { name: "Мария", age: 35 },
- { name: "Алексей", age: 73 },
- { name: "Яков", age: 12 },
- ];
- function byField(field, order) {
- var collator = new Intl.Collator(["en", "ru"], {
- numeric: true,
- });
- order = order ? 1 : -1;
- return function (a, b) {
- return order * collator.compare(a[field], b[field]);
- };
- }
- persons.sort(byField("age"));
- persons.sort(byField("name", false));
- // array map----------------------------------------------------------------------------------------------------------------------;
- {
- let prro = ["1", {}, null, undefined, "500", 700];
- let result = prro.map(function (item, index, arr) {
- var number = parseInt(item);
- return isNaN(number) ? item : number;
- });
- console.log(result);
- }
- // array reduce-------------------------------------------------------------------------------------------------------------------;
- {
- let arrReduce = ["0", 5, 3, "string", null];
- function numb(sum, i) {
- if (!isNaN(i) && isFinite(i) && i != 0 && i != null) {
- return (sum *= i);
- }
- return sum;
- }
- var result = arrReduce.reduce(numb, 1);
- console.log(result);
- }
- // object filter-------------------------------------------------------------------------------------------------------------------;
- {
- let laptop = {
- brand: "Lenovo",
- model: "Legion",
- RAM: "16 Gb",
- ROM: "512 Gb",
- processorModel: "Ryzen_5",
- numberOfCores: 6,
- };
- function filter(a, b, c) {
- for (var key in a) {
- if (key[0] === b) {
- delete a[key];
- } else if (a[key] === c) {
- delete a[key];
- }
- }
- return a;
- }
- console.log(filter(laptop, "b", 6));
- }
- // object map-------------------------------------------------------------------------------------------------------------------;
- function map(obj, funct) {
- for (key in obj) {
- funct(obj, key, obj[key]);
- delete obj[key];
- }
- return obj;
- }
- function newObj(obj, key, value) {
- obj[key + "^*&"] = value + "^^";
- return obj;
- }
- console.log(
- map({ brand: "Mercedes", model: "Vito", volume: 2.2, maxSpeed: 200 }, newObj)
- );
- // Рекурсия-------------------------------------------------------------------------------------------------------------------;
- // Sum-------------------------------------------------------------------------------------------------------------------;
- function sum(n) {
- if (n == 1) return 1;
- return n + sumTo(n - 1);
- }
- console.log(sum(55));
- // HTML Tree-------------------------------------------------------------------------------------------------------------------;
- let body = {
- tagName: "body",
- subTags: [
- {
- tagName: "div",
- subTags: [
- {
- tagName: "span",
- text: "Enter a data please:",
- },
- {
- tagName: "br",
- },
- {
- tagName: "input",
- attrs: {
- type: "text",
- id: "name",
- },
- },
- {
- tagName: "div",
- subTags: [
- {
- tagName: "input",
- attrs: {
- type: "text",
- id: "surname",
- },
- },
- {
- tagName: "div",
- subTags: [
- {
- tagName: "button",
- text: "Some text",
- attrs: {
- id: "newBtn",
- },
- },
- {
- tagName: "div",
- subTags: [
- {
- tagName: "div",
- subTags: [
- {
- tagName: "div",
- subTags: [
- {
- tagName: "input",
- attrs: {
- type: "date",
- id: "newDate",
- },
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- ],
- },
- {
- tagName: "div",
- subTags: [
- {
- tagName: "button",
- text: "OK",
- attrs: {
- id: "ok",
- },
- },
- {
- tagName: "button",
- text: "cancel",
- attrs: {
- id: "cancel",
- },
- },
- ],
- },
- ],
- };
- function createElem(tagName, attrs, text) {
- var elem = document.createElement(tagName);
- if (attrs) for (var name in attrs) elem.setAttribute(name, attrs[name]);
- if (text) elem.textContent = text;
- return elem;
- }
- function walker(tree) {
- var tagName = tree.tagName,
- attrs = tree.attrs,
- text = tree.text,
- elem = createElem(tagName, attrs, text);
- if (tree.subTags)
- tree.subTags.forEach(function (el) {
- el = walker(el);
- elem.appendChild(el);
- });
- return elem;
- }
- var table = walker(body);
- document.body.appendChild(table);
|