JS поиск по массиву

Какая-то *магия, делаю точно как в рабочем примере но не работает..

Вот массив:

 let myArray = [
 {n: "product1", c: 5, p: 10000.99},
 {n: "ProDuct2", c: 5, p: 5000.99},
 {n: "product3", c: 5, p: 8000.99},
 {n: "product4", c: 5, p: 430.66},
 {n: "product5", c: 5, p: 47535.45},
 {n: "product6", c: 5, p: 1111.23},
 {n: "tproduct7", c: 5, p: 5400.78},
  ];

Так ищу и вывожу. Выводит пустоту

  function filterByName(arr, name) {
    return arr.filter(function(item, i, arr) {
      return (item.n == name);
    });
  };

  let mespeackfrommyheart = filterByName(myArray, 'product4');
  console.log(mespeackfrommyheart);

Вот полный код:

function searchProduct(){

  let myArray = [
    {n: "product1", c: 5, p: 10000.99},
    {n: "ProDuct2", c: 5, p: 5000.99},
    {n: "product3", c: 5, p: 8000.99},
    {n: "product4", c: 5, p: 430.66},
    {n: "product5", c: 5, p: 47535.45},
    {n: "product6", c: 5, p: 1111.23},
    {n: "tproduct7", c: 5, p: 5400.78},
  ];
  function filterByName(arr, name) {
    return arr.filter(function(item, i, arr) {
      return (item.n == name);
    });
  };
  let mespeackfrommyheart = filterByName(myArray, 'product4');
  console.log(mespeackfrommyheart);

}
<input class="buttons" value="Search" onclick="searchProduct();" type="button">

А вот так, вызываю

<td class="left" ><input class="buttons" value="Search" onclick="searchProduct();" type="button"></td>

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Igor

Так работает. А как у Вас?

let myArray = [
 {n: "product1", c: 5, p: 10000.99},
 {n: "ProDuct2", c: 5, p: 5000.99},
 {n: "product3", c: 5, p: 8000.99},
 {n: "product4", c: 5, p: 430.66},
 {n: "product5", c: 5, p: 47535.45},
 {n: "product6", c: 5, p: 1111.23},
 {n: "tproduct7", c: 5, p: 5400.78},
];
  
function filterByName(arr, name) {
  return arr.filter(function(item, i, arr) {
    return (item.n == name);
  });
};

let mespeackfrommyheart = filterByName(myArray, 'product4');
console.log(JSON.stringify(mespeackfrommyheart));
console.log(mespeackfrommyheart);

→ Ссылка