Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Búsqueda Vanilla JavaScript: ¿cómo agregar varios campos?

Esta función busca desde un dato Json el campo "título". Por favor, ¿cómo puedo modificar esto para incluir varios campos, como: "etiquetas", "autor", etc.? ¡Gracias!

 document.addEventListener('DOMContentLoaded', function(event) { const search = document.getElementById('search'); const results = document.getElementById('results'); let data = []; let search_term = ''; fetch('/search.json') .then(response => response.json()) .then(data_server => { data = data_server; }); search.addEventListener('input', event => { search_term = event.target.value.toLowerCase(); showList(); }); const showList = () => { results.innerHTML = ''; if (search_term.length <= 0) return; const match = new RegExp(`${search_term}`, 'gi'); let result = data.filter(name => match.test(name.title)); if (result.length == 0) { const li = document.createElement('li'); li.innerHTML = `No results found 😢`; results.appendChild(li); } result.forEach(e => { const li = document.createElement('li'); li.innerHTML = `<a href="${e.url}">${e.title}</a>`; results.appendChild(li); }); }; });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

cambio

 let result = data.filter(name => match.test(name.title));

a

 let result = data.filter(name => match.test(name.title) || match.test(name.tags) || match.test(name.auther));
about 4 years ago · Juan Pablo Isaza Report

0

Puede ser una idea filtrar todas las entradas de los objetos dentro de la matriz recuperada del json.

Aquí hay un ejemplo mínimo reproducible , usando Event Delegation .

Ver también

 document.addEventListener(`click`, handle); const data = getJSONFakeData(); function handle(evt) { if (evt.target.id === `search`) { return searchJSON(); } } function searchJSON() { const resultsDiv = document.querySelector(`#results`); resultsDiv.textContent = ``; const nothingFound = isEmpty => resultsDiv.insertAdjacentHTML( `beforeend`, `<h3>${isEmpty ? `😢 No input` : `No results found 😢`}</h3>` ); const term = document.querySelector(`#term`).value.trim(); if (term.length < 1) { return nothingFound(true); } const re = new RegExp(term, `gi`); // filter here const results = data .filter( entry => Object.entries(entry) .find( ([, value]) => re.test(value) ) ); if (results.length) { let elems = []; results.forEach( result => { const res = Object.entries(result) .reduce( (acc, [key, value]) => acc.concat(`<i>${key}</i>: ${value};<br>`), ``); elems.push(`<li>${res}</li>`); }); return resultsDiv.insertAdjacentHTML( `beforeend`, `<ul>${elems.join(``)}</ul>`); } return nothingFound(); } function getJSONFakeData() { return [{ title: `title1`, author: `author1`, tags: `science, medicine`, editor: `Springer Verlag` }, { title: `title2`, author: `author2`, tags: `automotive, engine`, editor: `Elsevier` }, { title: `title3`, author: `author3`, tags: `programming, functional, loops`, editor: `Elsevier` }, ]; }
 body { font: normal 12px/15px verdana, arial; margin: 2em; }
 <input type="text" id="term" value="Elsevier"> <button id="search">Find</button> <div id="results"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!