How can a specific condition be used to remove an Object from an Array stored in localStorage?

I've saved some data in localStorage, for example:

$localStorage.recent = [{'id':1,'name':'abc','is_availbale':1},{'id':2,'name':'xyz','is_availbale':1},{'id':3,'name':'pqrs','is_availbale':0}];

There's another array that only contains the IDs of certain people (array_second can only have IDs already present in $localStorage.recent) -

array_second=['3'];

I need to remove the entries from $localStorage.recent that match the IDs in array_second. The expected output is-

$localStorage.recent = [{'id':1,'name':'abc','is_availbale':1},{'id':2,'name':'xyz','is_availbale':1}];

Answer №1

You are simply working with a regular array. The ngstorage library does not offer any extra features in this case.

Consider the following scenario:

$localStorage.recent = $localStorage.recent.filter((person) => {
    return second_array.indexOf(person.id) !== -1;
});

Answer №2

If you're looking for a helpful snippet of code in javascript, then this might be just what you need.

var dataArr = [{'id':1,'name':'John','is_availbale':1},{'id':2,'name':'Sara','is_availbale':1},{'id':3,'name':'Mike','is_availbale':0}];

var toRemove = [1];
for(var j=0; j<dataArr.length; j++){
    if(toRemove[0] == dataArr[j].id){
        dataArr.splice(j, 1);
    }
}

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

The process of passing parameter values by function in useEffect

Hi everyone, I hope you're all doing well. I'm currently facing an issue with trying to retrieve data from my API using the post method. The problem is that I can't use useEffect in any parameter. So, my workaround is to pass the data throug ...

Convert an array of objects into a single array of keys and multiple arrays of values using lodash

I have a large amount of data with many key-value pairs that I need to transmit. The keys are similar, so I prefer not to include them with each object. Let's say I have the following data: [ { x:11, y:12 },{ x:21, ...

Guide on automatically opening downloaded files in a new tab in IE 11 or Edge similar to the way file downloads function in Chrome

I am currently using Windows 10 and implementing msSaveOrOpenBlob in JavaScript to download the AJAX call blob on IE11. I want the PDF file to be opened in a new tab without any prompts, similar to how it works in Chrome. However, even when trying with th ...

Instead of receiving a response, an error is being displayed in the terminal

I'm currently developing a RESTful API using the Express framework. I've implemented a middleware, but for some reason, the error isn't being passed to the middleware as expected. I have code in the following files: in router.js router.get(& ...

What's the scoop on NodeJS, Express, Nginx, and Jade?

Currently exploring options for technologies and libraries to use in a new, large-scale project. With knowledge of NodeJS, JavaScript, Express, and Jade (now Pug), my team and I are leaning towards adopting these for the project. The main issue we're ...

Using NPM in conjunction with a PHP/STATIC web site directory structure: A comprehensive guide

My PHP website has a file structure like this: / css/ js/ index.php When I run the following commands: npm init npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb8984849f989f998a9babdfc5dd">[email p ...

Upload images easily by dragging and dropping a URL instead of a file

My goal is to make the following scenario possible: Picture this: two browser windows are open - one window (a) displaying a website with a drop area for picture files, and the other window (b) containing some pictures. I aim to drag and drop a picture d ...

What is the process for establishing a property within a component?

I have structured my form to display three steps sequentially by creating separate components for each step of the process. Here is a snippet from my app.js file: import LocationList from './components/LocationList.vue'; import ChooseTime from ...

Error TS2307: Module './images/logo.png' could not be located

I encountered an issue while attempting to import a local png image into my ts webpack project. The error message displayed was as follows: TS2307: Cannot find module './images/logo.png'. All other modules, such as css, svg, and ts files, impor ...

AngularJS UI-calendar eventSources not being properly refreshed

My issue is with the AngularJS UI-calendar not updating the $scope.eventSources data model after an event Drag and Drop. I have tried multiple solutions but nothing seems to work. Here is a snippet of my code: /* config object */ $scope.uiCon ...

Tips for reusing multiple sequences of chained transitions in D3

Looking for a more efficient way to code two lengthy sequences of chained transitions with varying order, properties, and attributes. For example, one sequence might be a, b, c, d, e, f, g, h, and the other e, f, g, h, a, b, c, d. Tried the code below with ...

Properties that cannot be modified, known as read-only properties,

While browsing through this post on read-only properties, I stumbled upon the following code snippet: var myObject = { get readOnlyProperty() { return 42; } }; alert(myObject.readOnlyProperty); // 42 myObject.readOnlyProperty = 5; // Assignment al ...

Executing ws.send from the controller in a Node.js environment

In my project, I am looking to send a websocket using express-ws from a different controller rather than through a route. In my server.js file, I have the following code: var SocketController = require('./routes/ws.routes'); var app = express(); ...

A different method to generate a random number between 0 and 9 in Angular 8

Looking for an alternative to Math.floor(Math.random()*10) in Angular 8? I need to remove math.random() due to security concerns. A suggestion was made to use PRNG, but I'm not familiar with how to do that. Any assistance would be greatly appreciated! ...

If a div element includes a specific text, then update that portion of the text without altering the value of any variables

Need help removing text without affecting variables Attempting to translate a specific line with JQuery Looking to change text only, leaving the content within the strong tag intact $('.content-box__row div:nth-child(3)').text('') ...

Error message: My custom configuration file (config.json) for Electron application is not being loaded

Recently, I started using Electron Framework and encountered a problem with accessing my custom config.json file in the project root from the main.js file. While everything worked fine when running the project using npm start, I ran into an error message w ...

Troubleshooting Google Authorization Issue in Angular 17: How to Fix the Error TS2304: 'google' Not Found in Angular 17

I am encountering an issue while attempting to integrate Google Auth into my Angular(+Express) application using the Google Identity Services library. Despite following the instructions provided in the Google tutorial, I am facing the error: "[ERROR] TS230 ...

Typing a Character Automatically Focuses on Input Field

I have a situation where I want to focus on an input field when the user presses the '/' key. The issue is that once the element is focused, it automatically adds the '/' key to the input field because of how the detection is set up. do ...

How to accurately determine the width of an element using JavaScript

Consider this scenario where I have created a custom dropdown list using HTML within a generic div: $(document).ready(function() { $("#selectbox-body").css("width", $("#selectbox").width()); }); <script src="https://ajax.googleapis.com/ajax/libs/ ...

Having issues with the integration of Sweet Alert and $_session variables

I am having trouble getting a sweet alert to show when the SQL condition is true. Below is the code for the back-end implementation. <?php include "../../../config/config.php"; if (isset($_POST['submit-slider'])) { $title = $_POST[&apos ...