Search within an Array to print match value

1

I have an array of users and I want to print in an html only the one with the email: "[email protected]", how can I do that search within the array.

var user = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771"
  }
  ];
    
asked by mserradas 17.03.2018 в 21:42
source

4 answers

1

Doing it with pure javascript would be something like this:

<html>
<body>
<div id="users">
</div>
<script>
var user = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771"
  }
  }
  ];
  
var text;
for (i = 0; i < user.length; i++) { 
	if(user[i].email === "[email protected]")
  {
  	//Imprimimos los elementos uno a uno:
    console.log(user[i].id);
    console.log(user[i].name);
    console.log(user[i].username);
    console.log(user[i].email);
    //Imprime el objeto JSON transformado a String
    document.getElementById("users").innerHTML = JSON.stringify(user[i], null, 4);
  }
}
</script>
</body>
</html>
   

You simply go through the array with for and if user[i].email is equal to the indicated value it is printed (if not, not).

In addition to accessing the elements you have the option to use JSON.stringify to see the entire object.

How you want to represent the data in the DOM will depend on your needs.

    
answered by 17.03.2018 в 23:44
1

Friend simply with a forEach you scroll through the array and ask if that item is equal (===) to the item you just want to print. If it is the element, print it and if not then do nothing or do something else (:

const user = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771"
    }
  },
  ]
  user.forEach((user) => {
    if(user.email === '[email protected]'){
      document.write('<p>' + user.email + '</p>')
    }
  });
    
answered by 18.03.2018 в 00:02
0

You can also use filter to go through user

var email = '[email protected]';
var result = '';

function findEmail (email) {
  var find = user.filter( function (item) {
    return item.email == email;
  })
  return find;
}

result = findEmail(email)[0];

console.log(result.name) //Leanne Graham
    
answered by 18.03.2018 в 17:21
0

For the management of arrays you can use the iteration functions that are defined in the Array prototype link

To address this example you can use for example: map, find, filter, forEach ...

For example:

user.map((val)=>{
    if(val.email=='[email protected]'){
        console.log(val);
    }
});

or

user.find((val)=>{return val.email=='[email protected]'})

or

user.filter((val)=>{return val.email=='[email protected]'})[0]

...

To understand how to use these functions of the Arrays in javascript then read the link that I left above.

If you do not want to use these functions you can always use the loops or cycles as for or while. For example:

  for(var i=0;i<user.length;i++){
    if(user[i].email=='[email protected]'){
        console.log(user[i])
    }
  }

or

  var i=0;
  while(i<user.length){
    if(user[i].email=='[email protected]'){
        console.log(user[i])
    }
    i++;
  }
    
answered by 01.05.2018 в 20:00