Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to check if a value exists in array with .includes?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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        
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

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

if not false

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!