Displaying properties of objects in javascript

Just starting with JavaScript and struggling to solve this problem. I attempted using map and filter but couldn't quite implement the if condition correctly. How can I find the records in the given array that have a gender of 0 and color is red?

let student = [
    {
        "ID": "1",
        "Name": "Senpai",
        "Gender": "1",
        "Class": "32",
        "Strength": "0",
        "Hairstyle": "1",
        "Color": "Black"
    },
    {
        "ID": "2",
        "Name": "Yui Rio",
        "Gender": "0",
        "Class": "11",
        "Strength": "0",
        "Hairstyle": "2",
        "Color": "Red"
    },
    {
        "ID": "3",
        "Name": "Yuna Hina",
        "Gender": "1",
        "Class": "12",
        "Strength": "0",
        "Hairstyle": "3",
        "Color": "Red"
    },
    {
        "ID": "4",
        "Name": "Koharu Hinata",
        "Gender": "0",
        "Class": "21",
        "Strength": "0",
        "Hairstyle": "4",
        "Color": "Green"
    },
    {
        "ID": "5",
        "Name": "Mei Mio",
        "Gender": "1",
        "Class": "22",
        "Strength": "0",
        "Hairstyle": "5",
        "Color": "Blue"
    }
];

Answer №1

let updatedArray = studentsList.filter(student => { if(student.Gender === "0" && student.Color === "Red"){ return student; } })

or

let updatedArray = studentsList.filter(student => student.Gender === "0" && student.Color === "Red")

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Press on the row using jQuery

Instead of using link-button in grid-view to display a popup, I modified the code so that when a user clicks on a row, the popup appears. However, after making this change, nothing happens when I click on a row. Any solutions? $(function () { $('[ ...

Should the use of readFileSync() during the initialization of a Node.js web application be avoided?

I have a Node app that serves web pages with static HTML-snippet files included conditionally. I am considering implementing a cache map for these snippets by adding the following code to my Express's app.js file: var cache = Object.create(null); cac ...

Accessing JSON Data Using JQUERY

Currently, I am experimenting with grabbing JSON data from websites using the $.getJSON() method. Here is the code snippet I have been working on: The website I am attempting to retrieve JSON data from can be found here. Interestingly, the code functions ...

What could be causing this error to appear when using Next.js middleware?

The Issue at Hand Currently, I am in the process of setting up an authentication system using Next.js, Prisma, and NextAuth's Email Provider strategy. My goal is to implement Next.js middleware to redirect any requests that do not have a valid sessio ...

What is a way to perform pre-increment without utilizing the ++I operator?

It is my belief that the code snippet below: i += 1 or i = i + 1 does not have the same effect as ++i. Am I incorrect in this assumption? Is there an alternative method to achieve pre-increment without utilizing the ++ operator? ...

I have chosen Next.js for my frontend development, while our backend developer is crafting the API in Express and MySQL. I am looking for ways to secure and protect the

As a beginner frontend developer learning Next.js, I've been tasked with integrating authentication into our app. The backend developer is working on creating the API using Express and MySQL. After successful login, an accessToken is received. However ...

Utilizing a ViewPager to showcase images sourced from a server

Looking to integrate a viewpager feature. I have images from a Rest API that need to be displayed within the view pager. Any suggestions or tips would be greatly appreciated to help me succeed in this task. ...

In order to ensure proper alignment, adjust the width of the select element to match the width of the

Is there a way for the select element to adjust its width based on the length of the selected option? I want the default option value's width to be shorter, but once another option is selected, I'd like the select element to resize itself so that ...

Typescript conversion: Changing a relational array into a tree object

My database is structured as an array of objects with the following format; array = [ {"name": "a", "id": "1", "parentId": NULL}, {"name": "b", "id": "2", "parentId": "1"}, {"name": "c", "id": "3", "parentId": "1"}, {"name": "d", "id": "4", "parentId" ...

Upon receiving the ajax request, the entire code page is called back

I'm having trouble saving data using a button in Laravel. When I use console.log, the entire code appears in the console and I don't understand why this is happening. Here is my input field and button: <form action="{{ url('sendcom& ...

retrieve JSON response from struts2 controller

My web application utilizes jquery and struts2. I am now facing the task of embedding a Google map into my webpage and adding some markers to it. To accomplish this, I have used the jquery.getJSON() command to send a request to a struts2 action. Here is a ...

Exploring DC2type with greenDao

In my Android application, I am currently using GreenDao and have a Contact Model with fields such as name, avatar, and phone number. The current requirement is to change from having only one phone number per contact to multiple phone numbers. Instead of ...

Check if jQuery condition is met for classes that match exactly

The PHP echo statement below contains the code. The brackets are empty, but I can guarantee that the statement inside, which modifies CSS, works - except for one issue. I want it to only target elements with a class attribute equal to "zoom" or "zoom firs ...

Avoid the need for users to manually input dates in the Custom Date Picker

After referencing this custom Date picker in ExtJs with only month and year field, I successfully implemented it. However, I am facing an issue where manual entry into the date field is not disabled. My goal is to restrict input for that field solely thr ...

Capture the response content and store it in a single variable within JMeter

How can I extract the authId value from the response of an Http Request in JMeter and assign it to a variable using a JSON extractor? Is the correct path $.ApiResponse.data.authId? The Content type is = > Content-Type: application/json; charset=utf-8 ...

Tabulator: the process of loading an extensive amount of data requires a significant amount of time

Currently, I am encountering an issue with loading data using a tabulator on my webpage. There are 38 tables that need to be populated, each containing approximately 2000 rows of data. The problem lies in the fact that it is taking an excessive amount of t ...

What is the best way to dynamically convert a lodash object (specifically a filter object) into jQuery's listview component?

After referencing the answer to this topic as my first step, which can be found here, my next goal is to utilize a filter function to retrieve all matching entries from a JSON file based on a search term. The objective is to loop through each match and con ...

The inconsistency of Selenium's StaleElementReferenceException error and the variability of pageload completion codes is causing issues with clicking on elements

Big shoutout to the amazing stackoverflow community for always providing assistance. Lately, I've been grappling with the frustrating "StaleElementReferenceException" issue and haven't found a universal solution yet. Some helpful members have rec ...

The error message "Cannot call expressjs listen on socket.ip" indicates that there

Currently working on a project involving websockets, but encountering an error in the code below: TypeError: require(...).listen is not a function Here's what I have tried so far: const app = require("express")(); const port = 3800; const ...