1. Define the following array of object in a module and export it. employees=[ { name:"raju", age:34, skills:["reactjs","angular","nodejs"], address:{ street:"kphb", city:"hyderabad" } }, { name:"bhanu", age:36, skills:["reactjs","angular","nodejs"], address:{ street:"miyapur", city:"hyderabad" } }, { name:"vikas", age:40, skills:["java","angular","python"], address:{ street:"koyambed", city:"chennai" } }, { name:"varun", age:58, skills:["reactjs","python","nodejs"], address:{ street:"time square", city:"warangal" } }, { name:"manasa", age:41, skills:["python","c#","dotnet"], address:{ street:"gachibowli", city:"hyderabad" } }]Import the above array of objects into another module , and then 1. Find the names(array of names) of employees of Hyderabad 2. Find the name and address of employees whose age between 40 and 50 3. Find the employees who are not from Hyderabad 4. Find the names (array of names) of employees whose skill is "reactjs"
Question
- Define the following array of object in a module and export it. employees=[ { name:"raju", age:34, skills:["reactjs","angular","nodejs"], address:{ street:"kphb", city:"hyderabad" } }, { name:"bhanu", age:36, skills:["reactjs","angular","nodejs"], address:{ street:"miyapur", city:"hyderabad" } }, { name:"vikas", age:40, skills:["java","angular","python"], address:{ street:"koyambed", city:"chennai" } }, { name:"varun", age:58, skills:["reactjs","python","nodejs"], address:{ street:"time square", city:"warangal" } }, { name:"manasa", age:41, skills:["python","c#","dotnet"], address:{ street:"gachibowli", city:"hyderabad" } }]Import the above array of objects into another module , and then 1. Find the names(array of names) of employees of Hyderabad 2. Find the name and address of employees whose age between 40 and 50 3. Find the employees who are not from Hyderabad 4. Find the names (array of names) of employees whose skill is "reactjs"
Solution
Here is how you can solve this in JavaScript:
- First, define the array of objects in a module and export it. This can be done in a file named
employees.js:
const employees = [
{
name: "raju",
age: 34,
skills: ["reactjs", "angular", "nodejs"],
address: {
street: "kphb",
city: "hyderabad"
}
},
// ... other employees
];
module.exports = employees;
- Then, import the array of objects into another module. This can be done in a file named
main.js:
const employees = require('./employees.js');
// 1. Find the names(array of names) of employees of Hyderabad
const hyderabadEmployees = employees.filter(employee => employee.address.city === 'hyderabad').map(employee => employee.name);
// 2. Find the name and address of employees whose age between 40 and 50
const agedEmployees = employees.filter(employee => employee.age >= 40 && employee.age <= 50).map(employee => ({name: employee.name, address: employee.address}));
// 3. Find the employees who are not from Hyderabad
const nonHyderabadEmployees = employees.filter(employee => employee.address.city !== 'hyderabad');
// 4. Find the names (array of names) of employees whose skill is "reactjs"
const reactEmployees = employees.filter(employee => employee.skills.includes('reactjs')).map(employee => employee.name);
Now, hyderabadEmployees is an array of names of employees from Hyderabad, agedEmployees is an array of objects with the name and address of employees aged between 40 and 50, nonHyderabadEmployees is an array of employees not from Hyderabad, and reactEmployees is an array of names of employees skilled in ReactJS.
Similar Questions
Use the map function to create objects from the arrays.The mapping should be done in such a way that for the first object, the first element of all arrays should be used, and so on.Input:const ids = [1, 2, 3];const names = ["John", "Jane", "Alice"];const images = ["john.jpg", "jane.jpg", "alice.jpg"];createUserObjects(ids,names,images);Output:[ { id: 1, name: 'John', image: 'john.jpg' }, { id: 2, name: 'Jane', image: 'jane.jpg' }, { id: 3, name: 'Alice', image: 'alice.jpg' }]
Problem statementSend feedbackYou are given three arrays containing the id, name, and image of users.ObjectivesCreate a function with a name createUserObjects that takes three arrays as a parameter and returns an array of objects representing the user. Use the map function to create objects from the arrays.The mapping should be done in such a way that for the first object, the first element of all arrays should be used, and so on.Input:const ids = [1, 2, 3];const names = ["John", "Jane", "Alice"];const images = ["john.jpg", "jane.jpg", "alice.jpg"];createUserObjects(ids,names,images);
Which of the following is a method that can be used to create a new array by applying a function to each element of an existing array in TypeScript?map()filter()reduce()slice()StatusCorrectMark obtained1/1Hints used0LevelHardQuestion typeMCQ Single CorrectSubjectProgrammingTopicAngularSub TopicTypescriptQuestion No: 2Multi Choice Type QuestionWhat is TypeScript?A markup language for web developmentA server-side scripting languageA type of database management systemA programming language developed by MicrosoftStatusCorrectMark obtained1/1Hints used0LevelEasyQuestion typeMCQ Single CorrectSubjectProgrammingTopicAngularSub TopicTypescriptQuestion No: 3Multi Choice Type QuestionWhich of the below syntax will clone and inject pieces of templated HTML snippets in the markup, removing it from the DOM when the condition evaluates to false.*ngIf="conditional"[hidden]="conditional"ngIf*="conditional"None of aboveStatusCorrectMark obtained1/1Hints used0LevelMediumQuestion typeMCQ Single CorrectShow solutionQuestion No: 4Multi Choice Type QuestionWhat is the index of the first element in an array in TypeScript?01-1NaNStatusCorrectMark obtained1/1Hints used0LevelEasyQuestion typeMCQ Single CorrectSubjectProgrammingTopicAngularSub TopicTypescriptQuestion No: 5Multi Choice Type QuestionWhich is the correct syntax of ngFor in Angular?*ngFor="let item of [1,2,3]"*ngFor="let item of 3"ngFor="let item of 3"ngFor="let item of [1,2,3]"
Create a function getStudentsByLocation that returns an array of objects who are located in a specific city.It should accept a list of students (from getListStudents) and a city (string) as parameters.You must use the filter function on the array.bob@dylan:~$ cat 2-main.jsimport getListStudents from "./0-get_list_students.js";import getStudentsByLocation from "./2-get_students_by_loc.js";const students = getListStudents();console.log(getStudentsByLocation(students, 'San Francisco'));bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~$
ES6 Iterate over array and find corresponding item in a different object
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.