-3

I have an array name JSON and how to check every department salary and finding the highest paid deparment

var json = [
   {
       id: 1,
       name: 'BInit',
       salary: 20000,
       department_slug: "it",
       department: 'IT'
   },
   {
       id: 2,
       name: 'BaaaInit',
       salary: 25000,
       department_slug: "hr",
       department: 'HR'
   },
   {
       id: 3,
       name: 'At',
       salary: 15000,
       department_slug: "finance",
       department: 'FInance'
   },
   {
       id: 4,
       name: 'aas',
       salary: 35000,
       department_slug: "finance",
       department: 'FInance'
   }
];

i will try this

for(var i=0; i<json.length; i++){
   for (var j= i; j<json.length; j++){

   }
}

I need highest salary department in this array

sayalok
  • 882
  • 3
  • 15
  • 30
lalit bhakuni
  • 607
  • 5
  • 15
  • Quick tip: use a variable to keep track of highest salary, if the current salary is higher than the value of variable update it with the current value else keep it as it is, also you don't need two loops – Code Maniac Aug 29 '19 at 12:17
  • 2
    Your question can have two outcomes. Either the answer is a department that has the highest total salary, or the department where one worker has the highest salary of the group. Which one do you want? Also, this is a *very* basic programming task, something _you_ should handle at your own easily. – KarelG Aug 29 '19 at 12:19
  • No need to use nested for which makes it O(n^2), it can be done in O(n). Simply consider a prob finding the largest number in an array. – Abhishek Mani Aug 29 '19 at 12:23
  • Please add an actual attempt to solve question not just a for loop just for the sake of it. – adiga Aug 29 '19 at 12:27
  • 1
    @KannanG that's not how sort works – adiga Aug 29 '19 at 12:38
  • Possible duplicate of [Finding the max value of an attribute in an array of objects](https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects) – Cerbrus Aug 29 '19 at 12:44

4 Answers4

0

EDIT: I realized that I did not completely understood the original question which stated: find the highest paid department

I have changed the code to find the highest paid departments (since it's possible that more than 1 department has the same total of salaries)

Note that I consider the attribute department_slug to act as an unique id.

var json = [
   {
       id: 1,
       name: 'BInit',
       salary: 20000,
       department_slug: "it",
       department: 'IT'
   },
   {
       id: 2,
       name: 'BaaaInit',
       salary: 25000,
       department_slug: "hr",
       department: 'HR'
   },
   {
       id: 3,
       name: 'At',
       salary: 15000,
       department_slug: "finance",
       department: 'FInance'
   },
   {
       id: 4,
       name: 'aas',
       salary: 35000,
       department_slug: "finance",
       department: 'FInance'
   }
];

var depTotalSalaries = {};

for(var i=0; i<json.length; i++) {
  if (!depTotalSalaries[json[i].department_slug]) {
    depTotalSalaries[json[i].department_slug] = json[i].salary;
  } else {
    depTotalSalaries[json[i].department_slug] += json[i].salary;
  }
}

var maxSalary = 0;
var departments = Object.keys(depTotalSalaries);
for(var i=0; i<departments.length; i++){
  maxSalary = Math.max(maxSalary, depTotalSalaries[departments[i]])
}

for(var i=0; i<departments.length; i++){
  if (depTotalSalaries[departments[i]] == maxSalary) {
    console.log(departments[i], maxSalary);
  }
}
Eric Villemure
  • 143
  • 2
  • 10
  • need height paid department not persone – lalit bhakuni Aug 29 '19 at 12:26
  • Ok I got you, you need the total of all salaries grouped by departments and then you need to find the department that has the maximum total salary right? – Eric Villemure Aug 29 '19 at 12:32
  • There are 3 loops here for something that can be done in 1 loop... Get the entry that has the highest salary, then log that one at the end. – Cerbrus Aug 29 '19 at 13:02
  • I guess I can do it in 2 loops you are right but it would only find the last highest paid department. Like I explained it's possible more than 1 department has the maximum total of salaries. In any case I would be interested to see how one could do it using only 1 loop. – Eric Villemure Aug 29 '19 at 13:05
0

What I would do, is something like this:

let highestSalarayDepartment = json.sort((a, b) => (parseInt(a.salary) > parseInt(b.salary)) ? -1 : +1)[0]

Source: https://flaviocopes.com/how-to-sort-array-of-objects-by-property-javascript/

-1

Use below example usingarray.reduce

var json = [
   {
       id: 1,
       name: 'BInit',
       salary: 20000,
       department_slug: "it",
       department: 'IT'
   },
   {
       id: 2,
       name: 'BaaaInit',
       salary: 25000,
       department_slug: "hr",
       department: 'HR'
   },
   {
       id: 3,
       name: 'At',
       salary: 15000,
       department_slug: "finance",
       department: 'FInance'
   },
   {
       id: 4,
       name: 'aas',
       salary: 35000,
       department_slug: "finance",
       department: 'FInance'
   }
];
const max = json.reduce(function(old, item) {
    return (old.y > item.y) ? old : item
}) 
console.log('max', max.salary);
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
-1

More old-school approach: Sort by salary, then take first element in sorted list:

var json = [
   {
       id: 1,
       name: 'BInit',
       salary: 20000,
       department_slug: "it",
       department: 'IT'
   },
   {
       id: 2,
       name: 'BaaaInit',
       salary: 25000,
       department_slug: "hr",
       department: 'HR'
   },
   {
       id: 3,
       name: 'At',
       salary: 15000,
       department_slug: "finance",
       department: 'FInance'
   },
   {
       id: 4,
       name: 'aas',
       salary: 35000,
       department_slug: "finance",
       department: 'FInance'
   }
];

var sorted = json.sort((a, b) => a.salary > b.salary ? -1 : 1)
// sorted[0] is the highest salary

Note, that Array.sort mutates the source, so if you care about the original sorting of your list, don't use this approach.

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • 2
    Why sort when you can just use a single loop and get the desired value which `O(n)` whereas sorting will take atleast `O(n logn)` – Code Maniac Aug 29 '19 at 12:22
  • Because it might have different upsides, if you actually need them ordered at a later time. You're absolutely right about performance, and as I've noted, there's mutation concerns as well. – Thor Jacobsen Aug 29 '19 at 12:24
  • need height paid department not height salary – lalit bhakuni Aug 29 '19 at 13:37