2

i have two arrays variables in which each element is an object having some properties like this :

var employees = [{
                 name: 'Jack',
                 empId: 0,
                 age: 25,
                 orgId: 1
                 }, {
                 name: 'Lucifer', 
                 empId: 1,
                 age: 35,
                 orgId: 2
                 }, {
                 name: 'Adam',
                 empId: 3,
                 age: 46,
                 orgId: 1
                 }, {
                 name: 'Eve',
                 empId: 4,
                 age: 30,
                 orgId: 3
               }];

and the second variable is

var companies= [{
 name: 'Microsoft',
 id: 1,
 employees: [5 , 9]
}, {
 name: 'Google',
 id: 2,
 employees: [1]
}, {
 name: 'LinkedIn',
 id: 3,
 employees: [10]
}];

so now i want that when i give a company name (for example: Google),then it will return the employee details. i want to do it by using filter()/reduce() method, but i am not able to do it . Help needed .. thank you

0

3 Answers 3

1

If the employee orgId is the same as the company id you can use filter; one to get the id, and then another to grab the employees associated with that id. The function returns an array of employees.

function getEmployees(company) {
  var id = companies.filter(function (el) {
      return el.name === company;
  })[0].id;
  return employee.filter(function (el) {
      return el.orgId === id;
  });
}

getEmployees('Microsoft');

OUTPUT

[
  {
    "name": "Jack",
    "empId": 0,
    "age": 25,
    "orgId": 1
  },
  {
    "name": "Adam",
    "empId": 3,
    "age": 46,
    "orgId": 1
  }
]

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

That's a good answer with a good demonstration of using the filter method
@Andy can u please tell me why u have given [0].id in the first filter ??
filter returns an array. Because that filter is only going to return one element matching the company name we grab the first element [0] which is an object, and then .id to get the id which we use in the second filter. @amark
1

You can do that with a forEach loop and checking the name:

var Employees = []; //initialize  
companies.forEach(function(company) {
    if (company.name == "Microsoft"){
        Employees = company.employees;
        //whatever you like
        Employees.forEach(function(id){
              alert(id);
        });

    }
});

Working JsFiddle: https://jsfiddle.net/sapyt777/

13 Comments

Be carefull with .forEach() method and its compatibility with IE8 and older browsers.
@MarcosPérezGude good point, but lets hope most people do not use ie8 anymore ;)
I wish God hears you. And IE9 / IE10 we must to kill them too. Long live to standards.
You might want to check that answer. The first line is commented and there's at least one typo.
@BasvanStein Just wanted to bring your attention to the break; as it's not compatible with forEach loops.
|
0

Trying to brush-up my skills. So I'm pretty sure it works but I don't know if it's a good way to achieve what you want!

var input = "Microsoft";

for(company in companies) {
    if(companies[company].name === input) {
        for(emp in employee) {
            if(companies[company].id === employee[emp].orgId)
            {
                console.log(employee[emp]);
            }
        }
    }
}

10 Comments

Please don't go changing the names of variables in the question if answers referencing that variable have already been posted. It breaks the code in those answers.
It just made good sense but you are right! .. Thanks for the advice, I updated my answer but not sure if I can cancel my pending edit!
Don't worry - I had exactly the same thought.
i am a newbie to javascript never touched before so im getting confused in so many things suppose if i made a function like this: function emp(name,companies,employees){ this.name = name; this.companies = companies; this.employee = employees; } now i create a object from the above function like : var emp1 = new emp('Microsoft',companies,emplyees); now i given the company name like this, then how to get the all employees for this
@amark my advice to you don't use 'this' for now as it a bit complicated and it's better to gain more ground before using it. I suggest you attend a couple of javascript courses in Udacity they are free and the Object Oriented Java course explains the 'This' parameter in details. For now my code will return an array of JSONs in console which you can view by using developer tools in a browser. You can post more details about the mark-up/HTML as well so we can better help with a more complete solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.