Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
How to get another field of same object in JSON with vanilla JS?

I have a JSON like this:

[
 {
  "title": "film1",
  "actor": ["jack", "fred"]
 },
 {
   "title": "film2",
   "actor": ["jack", "tom"]
 },
 {
  "title": "film3",
  "actor": ["albert", "luke"]
 }
]

There is an input element where the user can search inside this JSON. When the user type a string, the app should return a list of the corresponding "title" fields.

Example: The user types "jack". The app should return:

- film1
- film2

Because the string "jack" is only in the "actor" array of film1 and film2.

How do I code this? I think I should use Object.values and Array.prototype.reduce() but I'm not sure how.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

try this

var actor = "jack";
var films = [];
actors.forEach((item) => {
  if (item.actor.includes(actor)) films.push(item.title);
});
console.log(films);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Using JavaScript array methods

Use Array.filter and Array.map and Array.some for a one line solution. To make the search case-insensitive we also need to convert both the keyword and the actor name to uppercase before doing the compare.

films.filter(p => p.actor.some(name => name.toUpperCase() === keyword)).map(p => p.title);

Run the snippet to understand how it works

let films = [
 {
  "title": "film1",
  "actor": ["jack", "fred"]
 },
 {
   "title": "film2",
   "actor": ["jack", "tom"]
 },
 {
  "title": "film3",
  "actor": ["albert", "luke"]
 }
];


search.onchange = function(e) {

  let keyword = (search.value || "").trim().toUpperCase();
  
  let result =  films.filter(p => p.actor.some(name => name.toUpperCase() === keyword)).map(p => p.title);
  
  if (!result.length) result.push("No matches");
  
  output.innerHTML = "<li>" + result.join("</li><li>") + "</li>";
  
}

search.onchange();
<p>Enter a keyword and press enter:</p>
<input id="search" type="text" value="Jack">

<ul id="output"></ul>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda