Knowee
Questions
Features
Study Tools

Create a function getListStudentIds that returns an array of ids from a list of object.This function is taking one argument which is an array of objects - and this array is the same format as getListStudents from the previous task.If the argument is not an array, the function is returning an empty array.You must use the map function on the array.bob@dylan:~$ cat 1-main.jsimport getListStudentIds from "./1-get_list_student_ids.js";import getListStudents from "./0-get_list_students.js";console.log(getListStudentIds("hello"));console.log(getListStudentIds(getListStudents()));bob@dylan:~$ bob@dylan:~$ npm run dev 1-main.js [][ 1, 2, 5 ]bob@dylan:~$

Question

Create a function getListStudentIds that returns an array of ids from a list of object.This function is taking one argument which is an array of objects - and this array is the same format as getListStudents from the previous task.If the argument is not an array, the function is returning an empty array.You must use the map function on the array.bob@dylan:~cat1main.jsimportgetListStudentIdsfrom"./1getliststudentids.js";importgetListStudentsfrom"./0getliststudents.js";console.log(getListStudentIds("hello"));console.log(getListStudentIds(getListStudents()));bob@dylan:  cat 1-main.jsimport getListStudentIds from "./1-get_list_student_ids.js";import getListStudents from "./0-get_list_students.js";console.log(getListStudentIds("hello"));console.log(getListStudentIds(getListStudents()));bob@dylan:~ bob@dylan:~npmrundev1main.js[][1,2,5]bob@dylan:  npm run dev 1-main.js [][ 1, 2, 5 ]bob@dylan:~

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide to creating the function getListStudentIds in JavaScript:

  1. First, you need to define the function getListStudentIds that takes one argument, which is an array of objects.
function getListStudentIds(array) {
    // function body will go here
}
  1. Inside the function, you need to check if the argument is an array. If it's not, the function should return an empty array. You can use the Array.isArray() method to check if the argument is an array.
function getListStudentIds(array) {
    if (!Array.isArray(array)) {
        return [];
    }
    // rest of the function body will go here
}
  1. If the argument is an array, you need to use the map() function to create a new array that contains only the ids of the students. The map() function creates a new array with the results of calling a provided function on every element in the array.
function getListStudentIds(array) {
    if (!Array.isArray(array)) {
        return [];
    }
    return array.map(student => student.id);
}

In the above code, student => student.id is a function that takes a student object and returns the student's id. This function is called for each student in the array, and the map() function creates a new array with the returned ids.

  1. Now you can export the function using export default so that it can be imported in other files.
export default function getListStudentIds(array) {
    if (!Array.isArray(array)) {
        return [];
    }
    return array.map(student => student.id);
}

That's it! You have created the getListStudentIds function. You can now use this function to get an array of student ids from an array of student objects.

This problem has been solved

Similar Questions

Create a function named getListStudents that returns an array of objects.Each object should have three attributes: id (Number), firstName (String), and location (String).The array contains the following students in order:Guillaume, id: 1, in San FranciscoJames, id: 2, in ColumbiaSerena, id: 5, in San Franciscobob@dylan:~$ cat 0-main.jsimport getListStudents from "./0-get_list_students.js";console.log(getListStudents());bob@dylan:~$ bob@dylan:~$ npm run dev 0-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 2, firstName: 'James', location: 'Columbia' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~$

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:~$

1. Let's make some classroomsmandatoryImport the ClassRoom class from 0-classroom.js.Implement a function named initializeRooms. It should return an array of 3 ClassRoom objects with the sizes 19, 20, and 34 (in this order).bob@dylan:~$ cat 1-main.jsimport initializeRooms from './1-make_classrooms.js';console.log(initializeRooms());bob@dylan:~$ bob@dylan:~$ npm run dev 1-main.js [ ClassRoom { _maxStudentsSize: 19 }, ClassRoom { _maxStudentsSize: 20 }, ClassRoom { _maxStudentsSize: 34 }]bob@dylan:~$

2. A Course, Getters, and SettersmandatoryImplement a class named HolbertonCourse:Constructor attributes:name (String)length (Number)students (array of Strings)Make sure to verify the type of attributes during object creationEach attribute must be stored in an “underscore” attribute version (ex: name is stored in _name)Implement a getter and setter for each attribute.bob@dylan:~$ cat 2-main.jsimport HolbertonCourse from "./2-hbtn_course.js";const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"])console.log(c1.name);c1.name = "Python 101";console.log(c1);try { c1.name = 12;} catch(err) { console.log(err);}try { const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);}catch(err) { console.log(err);}bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js ES6HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ]}TypeError: Name must be a string ...TypeError: Length must be a number ...bob@dylan:~$

ES6 Iterate over array and find corresponding item in a different object

1/1

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.