Question

suppose i have an array in Javascript with several objects in it. var data = [...

suppose i have an array in Javascript with several objects in it.
var data = [
{
name: Jack,
mobile: 1234,
date: 2019-01-15 13:42:11},
}
{
name: Jack,
mobile: 1234,
date: 2019-01-13 10:35:55},
}
{
name: Jack,
mobile: 1234,
date: 2019-01-13 9:20:55},
}
{
name: Mike,
mobile: 1234,
date: 2019-01-13 12:35:55},
}
{
name: Mike,
mobile: 1234,
date: 2019-01-13 9:30:55},
}
{
name: Mike,
mobile: 1234,
date: 2019-01-13 05:25:55}
how to make a function that takes this array and returns the new array with objects, that only have the latest date. Notice that each object( name) has arrived at different time. so for each name, need the latest time to arrive
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code - Javascript

/* object array */

var data = [

    {

        "name": "Jack",

        "mobile": "1234",

        "date": "2019-01-15 13:42:11"

    },

    {

        "name": "Jack",

        "mobile": "1234",

        "date": "2019-01-13 10:35:55"

    },

    {

        "name": "Jack",

        "mobile": "1234",

        "date": "2019-01-13 9:20:55"

    },

    {

        "name": "Mike",

        "mobile": "1234",

        "date": "2019-01-13 12:35:55"

    },

    {

        "name": "Mike",

        "mobile": "1234",

        "date": " 2019-01-13 9:30:55"

    },

    {

        "name": "Mike",

        "mobile": "1234",

        "date": "2019-01-13 05:25:55"

    }]

/* function call filterArray */

filterArray(data);

/* it take json object as arguement */

function filterArray(data) {

    let maxDate;

    /* get the max date of Jack  uding filter and reduce fucntion javascript*/

    maxDateJack = data.filter((d) => d.name === "Jack").

        reduce((acc, prev) => {

            if (new Date(acc.date) > new Date(prev.date)) {

                maxDate = acc.date;

            }

            return maxDate;

        })

    console.log(maxDateJack)

    let maxDateMike;

    /* get the max date of Mike */

    maxDateMike = data.filter((d) => d.name === "Mike").

        reduce((acc, prev) => {

            if (new Date(acc.date) > new Date(prev.date)) {

                maxDateMike = acc.date;

            }

            return maxDateMike;

        })

    console.log(maxDateMike)

    /* get the max date of Jack and Mike using filter*/

    filterDate = data.filter((d) => {

        if (d.date == maxDateJack || d.date == maxDateMike) {

            return d;

        }

    })

    console.log(filterDate)

    return filterDate;

}

Add a comment
Know the answer?
Add Answer to:
suppose i have an array in Javascript with several objects in it. var data = [...
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
  • Hi, my JS function returns the new array with latest "time" objects and merging all "pics"...

    Hi, my JS function returns the new array with latest "time" objects and merging all "pics" and "videos". I just need to make an output of an array in order that was originally given(personnelMateriaRel, videos, contactor, pics, distributionResult, sendTime) var data = [ /*Object 1*/   {     "personnelMateriaRel": [],     "videos": [],     "contactor": {       "id": 18320,       "mobile": "13705139529",       "name": "Jack",       "otherTel2": "",       "temobile": "",       "workUnit": ""     },     "pics": [],     "distributionResult": {       "latitude": 23.095949110729155,       "longitude": 113.28544487112273,       "time": "2020-01-02 17:04:38",       "content": "Arrived",       "address": "USA"...

  • 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,...

  • LAB 4: JavaScript Loops Part I) 1) Make an array of colors, instead of responses, call...

    LAB 4: JavaScript Loops Part I) 1) Make an array of colors, instead of responses, call it colors. Color names go inside quotes, i.e. "red". 2) Copy the line of the var randomNum, but instead of looking at the length of responses, look at the length of your colors array. 3) Declare a variable myColor below the randomNum variable declaration. 4) Save a randomly chosen color into the variable myColor.     colors[randomNum] picks a random color from the array. Save...

  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

  • 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 =...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • Java Programming Create a program named MovieTest The program will create 10 Movie objects and place...

    Java Programming Create a program named MovieTest The program will create 10 Movie objects and place them in an array or ArrayList of type Movie. The program must do the following (all in main): Create an array or ArrayList of type Movie. Use the data in the text file that is in the download for the Movie file to create 10 Movie objects. Store the 10 Movie objects in the array or ArrayList. You MUST use the date in the...

  • i need help with this experiment, i have the rawa data and equation. Objects crucible cover...

    i need help with this experiment, i have the rawa data and equation. Objects crucible cover Magnesium mass (g) 20.5436 2.3947 0.4126 After 2nd heating object magnesium oxide mass (g) 0.4846 After 3rd heating object magnesium oxide mass (g) 0.5426 After 4th heating object magnesium oxide mass (g) 0.5716 M ..1 Metro by T-Mobile 6:17 AM Aa to Q 66 Experiment 6 The Law of Definite Proportions Reaction (6.1) will take place when a weighed, fixed amount of Mg(s) is...

  • I cant complete this assignment, I have tried several ways shown in the tutorial but cant...

    I cant complete this assignment, I have tried several ways shown in the tutorial but cant seem to get it to validate, can anyone help? This is a basic JavaScript tutorial and will not validate with Jquery. Monroe Public Library Denise Kruschev manages the website for the Monroe Public Library in Monroe, Ohio. One of her responsibilities is to add content and links to the site that will be of interest to the library’s patrons. Denise has asked you to...

  • The file containing the JAVA files, pseudocode file and doc file that have written for this...

    The file containing the JAVA files, pseudocode file and doc file that have written for this lab. Preamble The file releasedates.txt contains a list of video games and their release dates. Each line of the file contains the release date, a tab character, and then the name. The list is currently totally unsorted. The object of today's lab is to write a series of methods that allow us to perform the following tasks: read contents from a file and store...

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