最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Local Storage - Remove Key that partially contains string - Stack Overflow

programmeradmin6浏览0评论

I have given the option to my users to save certain information to local storage. So when they view an item and hit save, I create the key from the item ID. Here is an example of a key with 3 items added:

saved-items: ["31919988", "2424334", "6436366"]

And if they choose to remove a specific item, I splice the ID out of it where cid is the item ID:

names = JSON.parse(localStorage.getItem('saved-items'));
var index = names.indexOf(cid);
 if (index > -1) {
   names.splice(index, 1);
 }    
localStorage.setItem("saved-items", JSON.stringify(names));  

This has been working great but now I have a second Key that I need to remove when they choose to remove an item from the first key.

saved-items-price: ["31919988_5.88", "2424334_12.02", "6436366_04.32"]

So if the user chooses to remove the "31919988" item, I remove it from the first Key as above but I also need to remove it from the second key but indexOf naturally returns as false because it's not an exact match.

What would be the best way of matching the two key values together and removing both of them?

I have given the option to my users to save certain information to local storage. So when they view an item and hit save, I create the key from the item ID. Here is an example of a key with 3 items added:

saved-items: ["31919988", "2424334", "6436366"]

And if they choose to remove a specific item, I splice the ID out of it where cid is the item ID:

names = JSON.parse(localStorage.getItem('saved-items'));
var index = names.indexOf(cid);
 if (index > -1) {
   names.splice(index, 1);
 }    
localStorage.setItem("saved-items", JSON.stringify(names));  

This has been working great but now I have a second Key that I need to remove when they choose to remove an item from the first key.

saved-items-price: ["31919988_5.88", "2424334_12.02", "6436366_04.32"]

So if the user chooses to remove the "31919988" item, I remove it from the first Key as above but I also need to remove it from the second key but indexOf naturally returns as false because it's not an exact match.

What would be the best way of matching the two key values together and removing both of them?

Share Improve this question asked Feb 7, 2019 at 13:07 GenesisBitsGenesisBits 3463 silver badges28 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Why don't you just work with one single key? You could have an Object for each item and use properties like id and price to store your data.

// define items
var item1 = {id: 31919988, price: 5.88};
var item2 = {id: 2424334, price: 12.02};
var item3 = {id: 6436366, price: 04.32};
...
// save all items in an array in localStorage
localStorage.setItem('saved-items', JSON.stringify([item1,item2,item3]));
...
// get items for localStorage
var items = JSON.parse(localStorage.getItem('saved-items'));
// get items except for the one(s) you want to remove
var newItems = items.filter(item => item.id != 31919988);
// save back to localStorage
localStorage.setItem('saved-items', JSON.stringify(newItems));

You can simply string.split for each item of saved_item_price before processing it.

const names = JSON.parse(localStorage.getItem('saved-items-price'));
const updatedNames = names.filter(name => name.split('_')[0] !== cid);
localStorage.setItem("saved-items", JSON.stringify(updatedNames));  

You could get the key of the array like so :

var list = ["31919988_5.88", "2424334_12.02", "6436366_04.32"];
var search = "31919988"
var index = list.find(function(element, k) {
  return element.startsWith(search)
})
const key = Object.keys(list).find(key => list[key] === index);
console.log(key)

Try this:

var itemPrices = ["31919988_5.88", "2424334_12.02", "6436366_04.32"];
var newItemPrices = itemPrices.filter(item => item.indexOf("31919988") == -1);
let prices = ["31919988_5.88", "2424334_12.02", "6436366_04.32"];
let newPrices = prices.filter(item => !item.includes("31919988"));
发布评论

评论列表(0)

  1. 暂无评论