123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- ////Sort
- var persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- function sort(arr, key, boolean = true) {
- let middleArr = [];
- for (let i = 0; i < arr.length; i++) {
- middleArr.push(arr[i]);
- }
- for (let j = 0; j < middleArr.length; j++) {
- let index = 0
- for (let k = 1; k < middleArr.length;) {
- if (middleArr[index][key] < middleArr[k][key]) {
- [middleArr[index], middleArr[k]] = [middleArr[k], middleArr[index]]
- k += 2
- index += 1
- } else {
- k += 1
- index += 1
- }
- }
- }
- if (boolean == true) {
- middleArr.reverse()
- }
- return middleArr
- }
- console.log(sort(persons, "age"));
- console.log(sort(persons, "name", false))
- ///////Array map
- let array = ["1", {}, null, undefined, "500", 700]
- let newArray = array.map(function (num) {
- if (!num === false && !isNaN(+num)) {
- return +num
- }
- return num
- })
- //console.log(newArray)
- //Array reduce
- let reduceArray = ["0", 5, 3, "string", null]
- let result = reduceArray.reduce(function(a, b){
- if(typeof a !== "number"){
- a = b
- return a
- } else if
- (typeof b !== "number"){
- return a
- } else {
- a = a*b
- }
- return a
- })
- console.log(result)
|