Как получить доступ к свойству объекта в массиве и отсортировать массив по нему? - PullRequest
1 голос
/ 18 июня 2019

У меня есть задача - мне нужно отсортировать массив объектов по их свойствам. Свойство вставляется пользователем (как и все объекты), а затем они должны быть отсортированы по ключу, который должен быть равен имени параметра. Я вставляю все данные, и он не делает то, что должен. Пожалуйста, помогите мне найти решение.

   var employees = [];
   function Employee (name, sName, age, occupation) {
       this.name = name; 
       this.sName = sName;
       this.age = age; 
       this.occupation = occupation;
       this.show = function () {
            console.log(this.name + ' ' + this.sName + ' is ' 
            + this.age + ' years old, ' + 'and he/she is a ' + this.occupation);
       }
   } 



   function createNewEmployee () {
        var anotherEmployee; 

        for (i = 0; i < Infinity; i++) {
            var newEmployee = new Employee (prompt('First Name: '), prompt('Last Name: '), +prompt('Age: '),
            prompt('Job title(occupation): '));
            if (isNaN(newEmployee.age)) {
                alert('Age is a number! Try again!');
            } else {
                employees.push(newEmployee);
            }
            anotherEmployee = prompt('Add another employee? (y/n)');
            if (anotherEmployee == 'n') { 
                for (i = 0; i < employees.length; i++) {
                    employees[i].show();
                }
                break;
            }
        }
   }

   createNewEmployee();

   // 4 

   function addSalary (employees) {

       for (i = 0; i < employees.length; i++) {
            switch (employees[i].occupation) {
                case 'director': 
                    employees[i].salary = 3000;
                    break;
                case 'manager': 
                    employees[i].salary = 1500;
                    break;
                case 'programmer': 
                    employees[i].salary = 2000;
                    break;
                default: 
                    employees[i].salary = 1000;
                    break;
            }
       }
       for (i = 0; i < employees.length; i++) {
            employees[i].show = function () {
                console.log(this.name + ' ' + this.sName + ' is ' 
            + this.age + ' years old, ' + 'and he/she is a ' + this.occupation + '.' + ' ' + 'His/Her salary is ' + this.salary + '$');
            }
            employees[i].show();
        }
   }

   addSalary(employees);


// 5  

function employeeSorting () {
    var sortedElement = prompt('What parameter should be used for sorting? (options: name, sName, age, occupation, salary)');
    if (sortedElement in employees)
        {
            if (typeof(sortedElement) == 'string') {
            function compareString (a, b) {
                var nameA = a[sortedElement].toUpperCase();
                var nameB = b[sortedElement].toUpperCase();
                if (nameA > nameB) return 1;
                if (nameA < nameB) return -1;
                return 0;
            }
            employees.sort(compareString);
            return (employees);
        } else {
            function compareNumber (a, b) {
                if (a[sortedElement] < b[sortedElement]) return 1;
                if (a[sortedElement] > b[sortedElement]) return -1;
                return 0;
            }
            employees.sort(compareNumber);
            return (employees);
            }
        } else {
        alert('You have entered an invalid parameter, please try again');
        var sortedElement = prompt('What parameter should be used for sorting? (options: name, sName, age, occupation, salary)');
    }
} 

employeeSorting(employees);

employees.forEach(function(element) {
    element.show();
})

Ответы [ 2 ]

0 голосов
/ 18 июня 2019

Есть несколько проблем в вашей функции сортировки. Например, вы проверяете, существует ли имя свойства в массиве таких объектов: sortedElement in employees. Она не будет работать. Вам нужно перебрать этот массив, затем получить все ключи объекта данного элемента массива и проверить, содержат ли они указанное свойство или нет.

Я переписал эту функцию с нуля. Он работает как положено и может быть заменен напрямую без каких-либо изменений в других частях вашего кода:

function employeeSorting() {
    const sortedElement = prompt('What parameter should be used for sorting? (options: name, sName, age, occupation, salary)');
    try {
        employees = employees.sort((employee1, employee2) => { //Overwrites the global employees array
            const val1 = employee1[sortedElement];
            const val2 = employee2[sortedElement];
            if (val1 === undefined || val2 === undefined) { //If a non-existing property was entered, throw an error
                throw ('You have entered an invalid parameter, please try again');
            }
            if (!isNaN(val1) && !isNaN(val2)){ //Check numbers
                return parseInt(val1) < parseInt(val2) ? -1 : parseInt(val1) > parseInt(val2) ? 1 : 0;
            } else if (typeof val1 === 'string' && typeof val2 === 'string'){ //Check strings
                return val1.toUpperCase() < val2.toUpperCase() ? -1 : val1.toUpperCase() > val2.toUpperCase() ? 1 : 0;
            } else { //Both properties had different types
                throw('There\'s a data type inconsistency between employees');
            }
        });
    } catch (error) { //Ask for the correct property name again if an error was thrown
        alert(error);
        return employeeSorting();
    }
}
0 голосов
/ 18 июня 2019

Вот подход, который вы можете использовать, чтобы начать работу.Это функция, которая сортирует объекты с заданными вами свойствами и принимает имя свойства для сортировки (в настоящее время обрабатываются только целые числа и строки):

let data = [{ name: 'John', sName: 'Foo', age: 20, occupation: 'Developer', salary: 40000 },{ name: 'Bob', sName: 'Boo', age: 40, occupation: 'Chef', salary: 20000 },{ name: 'Mark', sName: 'Yu', age: 50, occupation: 'Manager', salary: 50000 }]

let sortBy = (arr, prop) => arr.sort((a,b) => Number.isInteger(a[prop])
  ? (a[prop] - b[prop])
  : a[prop].localeCompare(b[prop])
)

console.log(sortBy(data, 'age'))
console.log(sortBy(data, 'name'))
console.log(sortBy(data, 'occupation'))
console.log(sortBy(data, 'salary'))

Итак, ваша функция employeeSorting будет выглядеть примерно так:

function employeeSorting () {
  var sortedElement = prompt('What parameter should be used for sorting? (options: name, sName, age, occupation, salary)');
  if (sortedElement in employees)
    return sortBy(employees, sortedElement) // <-- use sortBy here 
  else
    alert('You have entered an invalid parameter, please try again');
} 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...