0

I have a small form where I enter values (id's and names) and store them in array by clicking a button, but I want to validate if there's an existing id key value in this array because I don't want to add repeated id key values.

Currently I have this:

<div class="form">
    <input type="text" id="idperson"><input type="text" id="name">
    <button id="add" type="button">ADD</button>
</div>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
    var people = [];

    $("#add").click(function(){
        var iduser = $("#idperson").val();
        var name = $("#name").val();

        if(people["idperson"]!=iduser){
            people.push({idperson:iduser,nameperson:name});
        }
        console.log(people);
    });
</script>

But it's not working because it continues storing repeated idperson values when I enter info in the form:

enter image description here

I'm sorry if I'm forgetting something, I'm newbie on this.

How can I fix it? I'd like some help.

3 Answers 3

1

The problem is that people is an array of object which contain idperson attribute, not the object itself.

This should do the trick :

var people = [];

$("#add").click(function(){
  var iduser = $("#idperson").val();
  var name = $("#name").val();

  if (!people.find(person => person.idperson === iduser)) {
    people.push({ idperson:iduser, nameperson:name });
  }

  console.log(people);
});
Sign up to request clarification or add additional context in comments.

6 Comments

I forgot to say I had to change if line closing parenthesis iduser))
My bad ... Edited
I have a question, what about if I want to remove element of array who has same iduser, how would be line code?
Remove the if block and change it for: people = people.filter(person => person.id !== iduser);
Ok, but in this case, how can I use splice in this kind of array?
|
1

You can check if the input data is exist in the people array or not exist. Check here the working code.

Thanks

var people = [];
$(document).ready(function(){
    

    $("#add").click(function(){
        var iduser = $("#idperson").val();
        var name = $("#name").val();

        if(checkPersion({idperson:iduser,nameperson:name}) && people["idperson"]!=iduser){
            people.push({idperson:iduser,nameperson:name});
        } else {
        	console.log('User exist');
        }
        console.log(people);
       
    });
    function checkPersion(obj){
		for(var i=0;i<people.length ;i++){
        	if(people[i].idperson == obj.idperson && people[i].nameperson == obj.nameperson ){
            	return false;
            }
        }
        return true;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
    <input type="text" id="idperson"><input type="text" id="name">
    <button id="add" type="button">ADD</button>
</div> 

Comments

0

You can use javascript objects

<script>
var people = {};

$("#add").click(function(){
    var iduser = $("#idperson").val();
    var name = $("#name").val();

    if(!(iduser in people)){
        people[iduser] = name;
    }
    console.log(people);
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.