//-----------------------------------------------------------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 } }