0

I know we can use .includes but I've been struggling to get it to work with my array. What I want is for my function to check if the value already exists and if it does to remove it from the array.

The value is a string. That value comes from an object that has .name as a property within the object.

0: {id: 190217270, node_id: 'MDEwOlJlcG9zaXRvcnkxOTAyMTcyNzA=', name: '3-Bit-CNC-Starter-Pack'}

1: {id: 187179414, node_id: 'MDEwOlJlcG9zaXRvcnkxODcxNzk0MTQ=', name: 'inb-go'}

I mapped through the data and assigned each button with a value of {d.name}

I am using a button to get the value with this function below

and adding the values to 'favs'.

const favs = [];

  function checkId(e) {
    if (e.target.value !== "")

      favs.push(e.target.value);

      localStorage.setItem("name", JSON.stringify(favs));
      console.log(favs);
      document.getElementById("favsarray").innerHTML = favs;
    }

console.log favs

[
    "3-Bit-CNC-Starter-Pack",
    "3-Bit-CNC-Starter-Pack"
]

How can I check to see if the value already exists within the array using .includes?

someone
  • 661
  • 1
  • 9
  • 26
  • 2
    `favs.includes(someValueHere)`? – VLAZ Feb 08 '22 at 12:17
  • yeah but where in the function ? Ive tried before .push and after and it doesn't check to see if 's there – someone Feb 08 '22 at 12:18
  • What value are you trying to check? Where is it being stored? – Terry Feb 08 '22 at 12:19
  • have a check now, i forgot to edit it into code. – someone Feb 08 '22 at 12:24
  • 1
    I have no idea where in the function you want it. I have no idea *what* are you trying to check. Nor what should happen after that. You asked for how to do the check - the obvious answer is with the code I showed you. If the question is not "how do I call `.includes()`" then you have not made that sufficiently clear, nor have you explained what is expected of a solution. – VLAZ Feb 08 '22 at 12:25
  • just added an extra line in to. I just want it to check if it is in the array and if it is to then remove it. – someone Feb 08 '22 at 12:28

2 Answers2

0

Just check before push:

function checkId(e) {
    if (e.target.value !== ""
        && !favs.includes(e.target.value)) 
    {
        favs.push(e.target.value); 
        // other code here        
    }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Probably makes sense to enclose other operations (saving to local storage, setting innerHTML) in the `if` block as well. – tromgy Feb 08 '22 at 12:30
  • Do you have any experience in removing a single value from localstorage? as you can see i am using the same key. But, I can't seem to remove the value in localstorage. – someone Feb 08 '22 at 12:38
  • @shar27 please, see this answer [How to delete an item from localStorage](https://stackoverflow.com/questions/9943220/how-to-delete-a-localstorage-item-when-the-browser-window-tab-is-closed) – StepUp Feb 08 '22 at 13:11
  • that answer didnt help sadly, would you mind checking my function if i post it up here? – someone Feb 08 '22 at 13:16
  • 1
    @shar27 as a good practise, it is better to have one question per topic. Please, ask your question by creating a new post. It will take more attention and help, if you will have another post. – StepUp Feb 08 '22 at 13:19
  • ive added it here - https://stackoverflow.com/questions/71034761/how-to-remove-one-value-from-a-key-in-localstorage-that-has-many-values – someone Feb 08 '22 at 13:30
-2

["Sam", "Great", "Sample", "High"].includes("Sam"); // true

if not false