I can not understand why this answer comes out in this cycle, please tell me.

$('input').keypress(function() { var obj = [{ name: "Loki", surname: "Man" }, { name: "Doki", surname: "Mon" }, { name: "Fuka", surname: "Don" } ]; var arr = obj.map((item) => { var otvet = item.name == 'Doki' ? console.log(item.name) : ''; console.log(otvet); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input /> 

    1 answer 1

    Because console.log('item') returns nothing.

    When the condition in the ternary operator is met, you write to the otvet variable the value returned by the call console.log(...); .

     $('input').keypress(function() { var obj = [ { name: "Loki", surname: "Man" }, { name: "Doki", surname: "Mon" }, { name: "Fuka", surname: "Don" } ]; var arr = obj.map(function(item) { if (item.name == 'Doki') return console.log(item.name); // надо return item.name; else return ''; }); console.log(JSON.stringify(arr)); }); 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input /> 


    Demonstration of how the variable otvet assigned the value undefined :

     function test() { } var otvet = true? test() : 'defined'; console.log(otvet); 

    • I'm sorry, I made a correction. - Chingiz Mamedov
    • one
      @ChingizMamedov, the essence has not changed - Grundy
    • @ChingizMamedov console.log(item.name) also returns nothing. - Igor
    • Then somebody will answer not with one word, but with a whole and clear sentence? - Chingiz Mamedov
    • one
      @ChingizMamedov I added a small example at the end showing where undefined comes from. - Igor