delete specific element in an array of local storage - javascript

Currently I have datas variable that contains multiple element values.
like this
["WLP001","WLP002","WLP003","WLP004","WLP022"]
Deleting datas variable will be possible like this localStorage.removeItem("datas");
But if I have a variable in my js code like this var item = "WLP022";
and delete only the WLP022 inside of that datas would it be possible?

You can get index of item to delete then just use splice to remove that item from array and set your datas again in localStorage.
Demo Code :
var to_delete = "WLP003"
//var datas = localStorage.getItem('datas');//parse it
//suppose this is data
var datas = ["WLP001", "WLP002", "WLP003", "WLP004", "WLP022"];
var index = datas.indexOf(to_delete);//get index
datas.splice(index, 1);//remove it
console.log(datas)
localStorage.setItem('datas', JSON.stringify(datas));//set again

Related

Delete specific string from Local Storage array

I have this code
function deleteElement() {
const myArray = map(listItems, getText);
var elementToDelete =document.getElementById('deleteElement').value;
const index = myArray.findIndex((item) => item.includes(elementToDelete));
if (index > -1) {
// delete and update local storage
console.log("found element and index ", index);
let moment = localStorage.getItem('pelis_guardades');
let deleted = moment.splice(index, 1);
localStorage.setItem('pelis_guardades', JSON.stringify(deleted))
console.log(deleted);
}
}
I have found the index of the element of the array that I want to delete, everything's good, but now I would like to "update" the local storage to delete the item from the index.
I can delete the specific value on the array that loads into the local Storage. Called myArray.
const myArray = map(listItems, getText);
myArray contains the "raw string data" that then gets put on the local Storage via,
localStorage.setItem('things',JSON.stringify(myArray));
How can I delete from the localStorage?
I've tried, the splice method on the local storage but doesn't work!!
Thanks!
try to parsing the moment variable to JSON
using
edit
function deleteElement() {
const myArray = map(listItems, getText);
var elementToDelete =document.getElementById('deleteElement').value;
const index = myArray.findIndex((item) => item.includes(elementToDelete));
if (index > -1) {
// delete and update local storage
console.log("found element and index ", index);
let moment = localStorage.getItem('pelis_guardades');
//try to add this code
let moment_parse = JSON.parse(moment);
let deleted = moment_parse.splice(index, 1);//edit
localStorage.setItem('pelis_guardades', JSON.stringify(deleted))
console.log(deleted);
}
before you splice the moment variable
The problem is that you've made a mistake using Array.splice.
This method mutates the given array.
You don't need the result of the splice operation. Instead you must pass the array as the new value to update the localstorage.
// 1. read value
const moment = JSON.parse(localStorage.getItem('pelis_guardades'))
// 2. mutate given array by removing one element from index.
moment.splice(index, 1);
// 3. write value
localStorage.setItem('pelis_guardades', JSON.stringify(moment))

How to iterate through an array stored in Local Storage JavaScript?

In this application I am getting this information from users in an HTML form: var orderName, trackingNumber, price the user is updating these dynamically. Then I'm storing them in an array, converting it to json format, and keeping the orderName variable (which the user gave earlier) as the local storage key,
var myArray = [];
myArray.push(orderName, trackingNumber,price);
var JsonOrder = JSON.stringify(myArray );
localStorage.setItem(orderName,JsonOrder );
Now I want to display all the orders and their information. One page with all the orders listed, and the orderName should be a HYPERLINK that takes user to a page with information on just that order.
Here is what I'm doing to loop through the array:
var savedOrders = JSON.parse(localStorage.getItem('orderName'));
var orderName;
var trackingNumber;
var price;
for (var key in savedOrders ) {
orderName = savedOrders[key][0];
trackingNumber = savedOrders[key][1];
price = savedOrders[key][2];
}
This isn't working?
simply do
let JsonOrder = JSON.stringify( [orderName, trackingNumber, price] );
localStorage.setItem('someName',JsonOrder );
//---
let orderName, trackingNumber, price;
[orderName, trackingNumber, price] = JSON.parse(localStorage.getItem('someName'));
I found an answer here:
// Get the existing data
var existing = localStorage.getItem('myFavoriteSandwich');
// If no existing data, create an array
// Otherwise, convert the localStorage string to an array
existing = existing ? existing.split(',') : [];
// Add new data to localStorage Array
existing.push('tuna');
// Save back to localStorage
localStorage.setItem('myFavoriteSandwich', existing.toString());
from https://gomakethings.com/how-to-update-localstorage-with-vanilla-javascript/
I think there is a better way then to use split since what if the user enters a [] or , as their input?But so far this works just not a good method.

How to generate one object key with an array of stored values from multiple on click events using localstorage and Jquery

I'm new to coding, and I need to display past search values from an input field using localstorage. The only way I can think of is by using one object key with an array of stored values from an on click event. Problem is, I can only get one position to appear as a value, with each value generated replacing the last. I've tried for loops and can't seem to get it to work. This is the code I have so far:
$('.search-city').on('click', function(e){
e.preventDefault();
var textArr = [];
var text = $(".form-control").val();
textArr.push(text);
localStorage.setItem("value1", textArr);
});
$('.search-city').on('click', function(e){
e.preventDefault();
var search = localStorage.getItem("value1")
This would work:
$('.search-city').on('click', function(e){
e.preventDefault();
// get the value from local storage
var localValue = localStorage.getItem('value1');
// if we had a value, parse it back to an array, if we dont, create an empty array
var textArr = localValue ? JSON.parse(localValue) : [];
// get the text from the search input, dont use "form-control"
// you're likely to have several of those on the page
// give the element a custom class like "search-input" and use that (id would be even better)
var text = $('.search-input').val();
// add the text to the array
text = trim(text);
if (text) {
textArr.push(text);
}
// enforce a size limit here by removing the 0 index item if the count has grown too large
var maxAllowed = 10;
while (textArr.length > maxAllowed) {
textArr.shift();
}
// localstorage can only hold simple strings so we'll JSON stringify our object and store that
localValue = JSON.stringify(textArr);
localStorage.setItem("value1", localValue);
});

index value cannot updated

Here I am using array to store date in local-storage. first add data that is stored after add another one the index value not increased. The newly add data only shows.
function save() {
var task = [];
localStorage.getItem('task');
task.push(document.getElementById("txt1").value);
console.log(task);
localStorage.setItem('array', task);
}

how to parsing multiple json based on its index

i have a script like this
$scope.asd = angular.fromJson($scope.asd[0].jfk);
but it only parse the object only on index 0,i want to parse all object based on the index on only at index 0 .
my pseucode
var i = get index;
for(k=0;k<i){
$scope.asd = angular.fromJson($scope.asd[0].jfk);
}
how to do it in angularjs?
You can do something like
var arr=angular.fromJson($scope.asd);
$scope.abc=arr.map(function(x){
return x.jfk;
});
//Now You can access all the jfk properties in $scope.abc like
console.log($scope.abc[i]);//i is the index
Or you can simply do
$scope.asd=angular.fromJson($scope.asd);
//And access properties like
console.log($scope.asd[i].jfk); //i is the index

Categories

Resources