0

I've tried to compare the values using for loop and after comparing the value of textbox with array elements it has to print exist or not exists if it is exist it has to show alert message and the value can't be added to the array if it is unique then the value has to be added to the Array

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<input type="text" id="text1"></input>
<input type="button" id="button1" value="Add Email" onclick="add_element_to_array();"> </input>
<p id="Result"></p>
<p id="r"></p>
<script>
var x = 0;
var array = Array();



function add_element_to_array()
{


function checkValue(value,array){
  var status = 'Not exist';

  for(var i=0; i<array.length; i++){
    var name = array[i];

    if(name == value){
      status = 'Exist';

    }
     document.getElementById("r").value = "status";
  }
}

 array[x] = document.getElementById("text1").value;
 alert("Element: " + array[x] + " Added at index " + x);
 x++;
 document.getElementById("text1").value = "";


 var e = "<hr/>";  

   for (var y=0; y<array.length; y++)
   {

   }
   document.getElementById("Result").innerHTML = e;
}

</script>

</body>
</html>
Yavuz Koca
  • 455
  • 4
  • 17
RaviChandra
  • 39
  • 1
  • 12
  • 1
    I think similar answer can be found below. Check this [link](https://stackoverflow.com/questions/36719477/array-push-and-unique-items/36719564) – Ansar Sep 05 '19 at 13:06

3 Answers3

1

You don't need to use a loop but can use includes() to check if an array contains a certain value.

Code example:

function checkValue(value, array) {
    var status = array.includes(value) ? 'Exist' : 'Not exist' ;
    document.getElementById("r").value = status;
}

Note that: IE browsers doesn't support includes(). In that case you can use indexOf()

var status = array.indexOf(value) !== -1 ? 'Exist' : 'Not exist';

indexOf() returns the index number if exist and -1 if not exist.


Also can try with Array.prototype.find().

MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

You can achieve this with findIndex() method

 const isPresent = !!(array.findIndex((f) => { return f.name === 'Neel' }) > -1);
 console.log(isPresent);

Check below snippet

 const array = [{ name: 'Neel'}, { name: 'Veer'}];
 const isPresent = !!(array.findIndex((f) => { return f.name === 'Neel' }) > -1);
 console.log(isPresent);

You can make it more dynamic by making a function

const isPresent = (array, key, value) => {
const isPresent = !!(array.findIndex((f) => { return f[key] === value }) > -1);
if(!isPresent){
   array.push({ [key]: value });
}
return { array, isPresent };
}

const array = [{ name: 'Neel'}, { name: 'Veer'}];

console.log(isPresent(array, 'name', 'Neel'));
console.log(isPresent(array, 'name', 'Neel2'));
console.log(isPresent(array, 'name', 'Neel3'));
Neel Rathod
  • 2,013
  • 12
  • 28
0

<h2>JavaScript Arrays</h2>
<input type="text" id="text1"></input>
<input type="button" id="button1" value="Add Email" onclick="add_element_to_array();"> </input>
<p id="Result"></p>
<p id="r"></p>
<script>
    var x = 0;
    var array = Array();



    function add_element_to_array() {


        function isValueInArrayAlready(value, array) {

            return array.indexOf(value) !== -1 ? true : false;
        }

        if(isValueInArrayAlready(document.getElementById("text1").value, array)) {
            alert("Element: " + document.getElementById("text1").value + " already exists, can't be added");
        } else {
            array[x] = document.getElementById("text1").value;
            alert("Element: " + array[x] + " Added at index " + x);
            x++;
        }

        document.getElementById("text1").value = "";


        var e = "<hr/>";

        for (var y = 0; y < array.length; y++) {

        }
        document.getElementById("Result").innerHTML = e;
    }

</script>

Javier Aviles
  • 7,952
  • 2
  • 22
  • 28