How to display the value of an object by its key?

  • 0
    I have an object that contains information about a person.
    I tried to make a function that displays the value of an object by its key (that is, I enter name and displays the information of this key), but it displays the entire object.
    Here's the code itself:
    let Student = { name: "Никита", sur: "Гвоздов", age: 19};
    function search()
    {
      let key = prompt("Введите значение")
      for (key in Student) {
      alert( "Ключ: " + key + " значение: " + Student[key] );
    }
    }
    search()
    JavaScript Autumn Patrick, Oct 24, 2020

  • 3 Answers
  • 0
    let Student = { name: "Никита", sur: "Гвоздов", age: 19};
    function search()
    {
    let key = prompt("Введите значение")
    alert( "Ключ: " + key + " значение: " + Student[key] );
    }
    search()
    Anonymous

  • 0
    (() => {
    let Student = {
    name: "Никита",
    sur: "Гвоздов",
    age: 19
    };
    let key = prompt("Введите значение", '');
    if (Student[key]) {
    alert("Ключ: " + key + " значение: " + Student[key]);
    } else {
    alert('Не найдено');
    }
    })()
    Layla Tucker

  • 0
    why go through the cycle? remove the loop
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!