Question

I have this module of javascript var data = [     {id: 1, firstName: 'John', lastName: 'Smith'},...

I have this module of javascript

var data = [

    {id: 1, firstName: 'John', lastName: 'Smith'},

    {id: 2, firstName: 'Jane', lastName: 'Smith'},

    {id: 3, firstName: 'John', lastName: 'Doe'}

];

module.exports.setid =

    function (value) {

        data.id = value;

};

module.exports.getid =

function () {

        return data.id;

    };

module.exports.setFirstName =

function (value) {

        data.firstName = value;

    };

module.exports.getFirstName =

function () {

        return data.firstName;

    };

module.exports.setLastName =

function (value) {

        data.lastName = value;

    };

I wrote the following to get all first and last names to show but the only out put i get

var foo = require('./EmployeeModule_v1');

console.log(foo.getFirstName(),

                     foo.getLastName(Smith));

out put

PS C:\Users\play_\OneDrive\Documents\scchool\Server_side\m1> node hw1a
undefined undefined

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your current program is outputting undefined because your data is an array of objects. You are returning data.firstname but you are not giving from which index it should return the firstname. Your data array does not have any firstname property. You need to use forEach method to loop through the items and store the values in an array and return the array.

Kindly use the methods below to retrieve the value in an array format.

// Get all the firstnames in an array

function getFirstName(){

var firstname = [];

data.forEach(function(item){

firstname.push(item.firstName);

});

return firstname;

}

-----------------------------------------------------------------------------------------

// To Set Firstname, we need to pass the unique identifier, which is ID in this case.

function setFirstName(id, value){

data.forEach(function(item){

if(item.id === id){

item.firstName = value;

console.log(item.firstName);

}

});

}

---------------------------------------------------------------------------------

// Get all the lastnames in an array

function getLastName(){

var lastname = [];

data.forEach(function(item){

lastname.push(item.lastName);

});

return lastname;

}

--------------------------------------------------------------------------------------------------------------

// To Set Lastname, we need to pass the unique identifier, which is ID in this case.

function setLastName(id, value){

data.forEach(function(item){

if(item.id === id){

item.lastName = value;

}

});

}

----------------------------------------------------------------------------------------------------------

// Get all the lastnames in an array based on firstname

function getLastName(value){

var lastname = [];

data.forEach(function(item){

if(item.firstName === value)

lastname.push(item.lastName);

});

return lastname;

}

Add a comment
Know the answer?
Add Answer to:
I have this module of javascript var data = [     {id: 1, firstName: 'John', lastName: 'Smith'},...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add comments to it by filling in the blank comments numbers 1 through 32. (The first...

    Add comments to it by filling in the blank comments numbers 1 through 32. (The first few comments are HTML comments, not JavaScript comments, and are bracketed according to <!- this is an HTML comment>.) <!DOCTYPE html> <html> <body onload="handleFormAction()"> <h2>Form handling example in JavaScript</h2> <h3>(Or, John Doe meets Robbie Robot)</h3> <h4>Instructions: Fill in the form. Then fill in comments 1 through 23 in the JavaScript code!</h4> <form target="_blank" action= > <!- 1: replace this placeholder HTML comment with a...

  • Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on...

    Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on the client side before it is submitted to the server. It is done everywhere because it is fast and it gives you a great deal of flexibility in how you handle errors insofar as the GUI is concerned. Attached is an image of some code I wrote (so Blackboard can't mess it up). Some things to notice that will help you with the lab....

  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • javascript questions Declare an empty deck array. Use a loop to fill the array with string values that represent the 52 cards of a normal deck. The numbers go from 1 to 13 with 1 for an ace, 11 for...

    javascript questions Declare an empty deck array. Use a loop to fill the array with string values that represent the 52 cards of a normal deck. The numbers go from 1 to 13 with 1 for an ace, 11 for a jack, 12 for a queen, and 13 for a king. The suits are C for clubs, D for diamonds, H for hearts, and S for spades. Here’s the list of 52 values: 1C, …, 13C, 1D, …, 13D, 1H,...

  • I have the code buts its not working as this below instruction can anyone help Use...

    I have the code buts its not working as this below instruction can anyone help Use the requirements from the pizza part of Assignment 6 and do the following using CSS and an internal style sheet: 1.     Your Javascript function should be in an external file (don’t forget to move it up to studentweb too) 2.     Put a red border of 300px in width around the form 3.     There should be three Div sections: header, main_body, and footer. 4.     Center the name of your...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • I wrote code in C++ that takes a text file containing information on different football players...

    I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT