-1

I'm new to coding and have been working on some example problems for practice...

For this question I was to to create an array of objects for 10 employees and assign them name, age, position, and salary. Then, I was to find the salaries of all developers. I was able to do this but, I was wondering how to then convert the salaries to an array.

Code and what it displays in the console below:

const people = [
    {name: "bob", age: 20, position: "developer", salary: 120_000},
    {name: "susy", age: 26, position: "designer", salary: 140_000},
    {name: "peter", age: 33, position: "developer", salary: 220_000},
    {name: "cody", age: 51, position: "boss", salary: 400_000},
    {name: "franklin", age: 22, position: "designer", salary: 150_000},
    {name: "anna", age: 27, position: "developer", salary: 175_000},
    {name: "justin", age: 35, position: "developer", salary: 230_000},
    {name: "charlie", age: 23, position: "designer", salary: 120_000},
    {name: "tony", age: 38, position: "designer", salary: 210_000},
    {name: "ruth", age: 41, position: "developer", salary: 300_000}
];

function displayDeveloperSalaries(array){
const developers = people.filter(person => person.position === "developer");
for (let i = 0; i < developers.length; i++) {
    console.log(developers[i].salary);
}
}
displayDeveloperSalaries(people);
120000
220000
175000
230000
300000

Any help is much appreciated. Thanks!

Not exactly how to convert the returned values to an array... I was able to push the values each to their own array like this,

[ 120000 ]
[ 220000 ]
[ 175000 ]
[ 230000 ]
[ 300000 ]

but I'm not sure how to concat these into a new array or what the best method is.

kewl
  • 1
  • 2
  • also: [use filter to return property values in an object](https://stackoverflow.com/questions/31201262/use-filter-to-return-property-values-in-an-object) – pilchard Dec 18 '22 at 18:34
  • 1
    (#kewl) you actually did 95% of the work, please check this line if it provides what you ask for `let SalaryArray = people.filter(p => p.position == "developer").map(m => m.salary);`. – Bola Adel Nassif Dec 18 '22 at 18:46
  • Yes! Thank you! This is exactly it. I wasn't too familiar with array.map() before. I posted this in my answer so everyone can see the full solution. – kewl Dec 18 '22 at 19:13

3 Answers3

0

This is exactly what map was made for:

const people=[{name:"bob",age:20,position:"developer",salary:12e4},{name:"susy",age:26,position:"designer",salary:14e4},{name:"peter",age:33,position:"developer",salary:22e4},{name:"cody",age:51,position:"boss",salary:4e5},{name:"franklin",age:22,position:"designer",salary:15e4},{name:"anna",age:27,position:"developer",salary:175e3},{name:"justin",age:35,position:"developer",salary:23e4},{name:"charlie",age:23,position:"designer",salary:12e4},{name:"tony",age:38,position:"designer",salary:21e4},{name:"ruth",age:41,position:"developer",salary:3e5}];

const salaries = people.map(e => e.salary)
console.log(salaries)

And to sum, you can use reduce:

const people=[{name:"bob",age:20,position:"developer",salary:12e4},{name:"susy",age:26,position:"designer",salary:14e4},{name:"peter",age:33,position:"developer",salary:22e4},{name:"cody",age:51,position:"boss",salary:4e5},{name:"franklin",age:22,position:"designer",salary:15e4},{name:"anna",age:27,position:"developer",salary:175e3},{name:"justin",age:35,position:"developer",salary:23e4},{name:"charlie",age:23,position:"designer",salary:12e4},{name:"tony",age:38,position:"designer",salary:21e4},{name:"ruth",age:41,position:"developer",salary:3e5}];

const salaries = people.map(e => e.salary)
const sum = salaries.reduce((acc, curr) => acc + curr, 0)
console.log(sum)
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    Thank you for your help! I see this gets the total of all developer salaries... I actually wanted to display each developer salary into a new array. Thanks! – kewl Dec 18 '22 at 18:37
  • @kewl Ah, then you could use the first code snippet to get an array of the salaries. – Spectric Dec 18 '22 at 18:38
  • @Spectric I see. Thank you for your help! I was able to filter the people array into a developers array and map the salaries from the developers array. I posted the full solution so everyone can see. – kewl Dec 18 '22 at 19:04
0
const people = [
    {name: "bob", age: 20, position: "developer", salary: 120_000},
    {name: "susy", age: 26, position: "designer", salary: 140_000},
    {name: "peter", age: 33, position: "developer", salary: 220_000},
    {name: "cody", age: 51, position: "boss", salary: 400_000},
    {name: "franklin", age: 22, position: "designer", salary: 150_000},
    {name: "anna", age: 27, position: "developer", salary: 175_000},
    {name: "justin", age: 35, position: "developer", salary: 230_000},
    {name: "charlie", age: 23, position: "designer", salary: 120_000},
    {name: "tony", age: 38, position: "designer", salary: 210_000},
    {name: "ruth", age: 41, position: "developer", salary: 300_000}
];

// Map  Developer Salaries
const developers = people.filter(person => person.position === "developer");
const developerSalaries = developers.map(e => e.salary)
console.log(developerSalaries);
kewl
  • 1
  • 2
0

Adding all the data into the array would be the best option. Using ForEach function for iterating in the array and pushing the necessary data if the condition meets to the output array.

const people = [
    {name: "bob", age: 20, position: "developer", salary: 120000},
    {name: "susy", age: 26, position: "designer", salary: 140000},
    {name: "peter", age: 33, position: "developer", salary: 220000},
    {name: "cody", age: 51, position: "boss", salary: 400000},
    {name: "franklin", age: 22, position: "designer", salary: 150000},
    {name: "anna", age: 27, position: "developer", salary: 175000},
    {name: "justin", age: 35, position: "developer", salary: 230000},
    {name: "charlie", age: 23, position: "designer", salary: 120000},
    {name: "tony", age: 38, position: "designer", salary: 210000},
    {name: "ruth", age: 41, position: "developer", salary: 300000}
];


function displayDeveloperSalaries(array){
    let salaryArray=[]
    array.forEach(person=> {
    if(person.position=="developer")
    {
        salaryArray.push(person.salary);
    }
})
console.log("salary",salaryArray)
}
displayDeveloperSalaries(people);
  • Thank you! I thought about using forEach too but I wasn't exactly sure how to do it. – kewl Dec 18 '22 at 19:09