What is the best way to eliminate several values from an array in javascript?

var array = [{
    name: "Mango",
    weight: "15gm"
}, {
    name: "Banana",
    weight: "10gm"
}, {
    name: "Apple",
    weight: "15gm"
}, {
    name: "Grapes",
    weight: "5gm"
}, {
    name: "Banana",
    weight: "15gm"
}];

I need to filter out everything that is not Banana.

Answer №1

Check out more information here.

array.filter(function(x) {
    return x.name == "Banana";
});

If we have the following data:

[{"name":"Mango","weight":"15gm"},{"name":"Banana","weight":"10gm"},{"name":"Apple","weight":"15gm"},{"name":"Grapes","weight":"5gm"},{"name":"Banana","weight":"15gm"}]

We should expect to see this result:

[{"name":"Banana","weight":"10gm"},{"name":"Banana","weight":"15gm"}]

Answer №2

After removing elements from an array using the splice method, the length of the array is decreased by 1. It is important to take into account the index when performing such operations. Below is a snippet of code that demonstrates how to remove specific objects from an array.

for(let j=0; j<fruits.length; j++){
   if(fruits[j].type !== "Apple"){
       fruits.splice(j,1);
       j--;
   }
}

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

Clipped Words & Silhouettes

I'm having trouble ensuring the text in this particular example displays correctly. I am experiencing challenges with the clipping of text and shadows on certain letters, and I'm struggling to identify the root cause as well as the solution. In ...

React.js - keeping old array intact while using array map

I am currently experiencing an issue where I have two arrays, and I need to map through one of the arrays when navigating the page. However, instead of replacing the old array with the new one, it is just keeping the old array and adding the new one. Here ...

the click event fails to trigger when the id or class is modified

Hey there, I'm new to working with jquery and I've encountered a problem. Here's the code snippet: http://jsfiddle.net/8guzD/ $('#test.off').click(function(){ $(this).removeClass('off').addClass('on'); }) ...

Extracting specific key-value pairs from JSON data

Working with JSON data, I encountered a need to pass only specific key-value pairs as data to a component. Initially, I resorted to using the delete method to remove unwanted key-value pairs, which did the job but left me feeling unsatisfied. What I truly ...

Resetting ajax success message when modal is closed

Currently utilizing w3.css and its modal feature, I am encountering an issue with clearing the message displayed after a successful ajax call once the window is closed. Even though I have implemented code to clear the message upon closing the window (by cl ...

Setting up a Node.js application with Nginx on DigitalOcean

While running my application on a DigitalOcean droplet using nginx, I encountered a peculiar issue. The app runs perfectly fine with http, but when switching to https, nginx throws a 502 BAD GATEWAY error. Despite trying various DigitalOcean guides and sco ...

The input value fails to update after the method is called

I am working on developing a todo-list application and here is the code I have so far: HTML: <div class="divPadder"> <input ref="makePlaceholderEmpty" class="inputBoxSize" type="text" :placeholder="placeholder"v-model="task"> <ul> ...

Updating a static array in VB.NET: A step-by-step guide

I am currently in the process of converting VBA6 code to VB.net within Visual Studio 2105. In my VBA6 code, I load an array with lines from a file and keep it for repeated use like this: Static sArr as Variant 'Load the file to a String called S (de ...

Working with jQuery: Retrieving the ID of an element that is generated dynamically

Looking for some guidance: I'm working on dynamically creating a table based on the response from an AJAX call. The table includes headers and rows, with new rows added using the "after" function. However, I'm facing an issue with accessing the ...

HTML Menu with Ajax functionality designed to work seamlessly without relying on JavaScript

I'm working on making my navigation system compatible with both clients who have JavaScript disabled and those who can use Ajax. Currently, I have dynamic links generated inside the navigation like "index.php?page=/cat/page.php". <li id="sidebarit ...

Implement a dialog on top of a current web page, achieve php-ajax query result, and enhance with

My website features 'dynamic' content, where 'static' nav-buttons replace specific div contents upon clicking. While I am able to retrieve my php/ajax results in a dialog box, I am struggling with placing this dialog above my current p ...

What is the best way to retrieve and display the heading of the current section that is being viewed using Bootstrap Vue's ScrollSpy feature?

I am currently using the Bootstrap Vue Scrollspy feature in my project with Nuxt js. The elements are correctly referenced and are successfully spying on the content. What I would like to achieve is having the ability to update a specific div with the curr ...

Issue with Material UI components: The Select component is collapsed and the autoWidth functionality is not

The Material UI (React) Select component is not expanding in width as expected, even with the autoWidth property. https://i.sstatic.net/h3H0V.png <FormControl margin="dense"> <InputLabel id="prefix-label">Prefi ...

Utilize dropdown1 to dynamically populate dropdown 2 in AngularJS

Here is the HTML code snippet I am currently working with: <select ng-controller="category" ng-model="selectedTestAccount" ng-options="c.category for c in categories track by c.categoryID" ></select> <select ng-controller="subcategory" ng ...

Transmit an array of predetermined size to a function using a pointer parameter

Is there a way to modify the provided code to make it work efficiently, or will I need to write a new function specifically for fixed sizes? If I do need to create a new function, how can I make it generic enough to handle various fixed sizes? void displa ...

React Error: Unable to Call function this.state.Data.map

I'm encountering an issue with mapping objects in ReactJS. Even though I am able to fetch data, when I try to map it, I receive the following error: this.state.Data.map is not a function Here's the code snippet: import React, { Component } fro ...

Display loading images using the Bxslider plugin

Currently, I have implemented a Bxslider on my website with the following HTML markup: <div class="slide"> <a target="_blank" href="#"><img src="image.jpg"/></a> </div> <div class="slide"> <a target="_blank" href= ...

Navigating Next.js: Mastering the art of localStorage Access

Currently, I am developing my first member area using next.js. My goal is to store some data in localStorage (such as token, expiresAt, userInfo), which will eventually be moved to an http-only cookie. The code below is generating the error: "LocalStorage ...

Issue with deep linking functionality on S3 storage service turning out to be

After successfully deploying my angular5 app to: http://myApp.s3domain.amazonaws.com The Angular router is automatically directing me to http://myApp.s3domain.amazonaws.com/home However, when I try to access a link with a unique parameter like so: http:/ ...

Unable to display canvas background image upon webpage loading

Currently working on a JavaScript project to create a captcha display on a canvas. The issue I'm facing is that the background image does not load when the page initially opens. However, upon hitting the refresh button, it functions as intended. Here& ...