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

132
Views
Búsqueda mundial de JSON

Estoy creando una función para buscar información en una estructura json, mi función funciona bien en el primer nivel pero tengo problemas para buscar en el segundo nivel. Mi función devuelve todo el objeto json como si la búsqueda nunca se hubiera realizado.

este es mi código:

json:

 let machine = [ { "sku": "qweert-12", "type": "aaaa", "components":[ { "unit": "mmmm", "sku": "qwer-12" }, { "unit": "llll", "sku": "qwer-14" } ] }, { "sku": "qmqert-12", "type": "bbbb", "components":[ { "unit": "ssss", "sku": "qwlr-12" }, { "unit": "jjjj", "sku": "qwuer-14" } ] }, ]

función

 const search= (inputUser, data, setReturninfo) => { const input = inputUser.target.value(); const result = data.filter((data) => { let component= data.components; return Object.keys(component).some((key) => { return JSON.stringify((data[key])).toLocaleLowerCase().trim.includes(input); }) }); setReturninfo(result); }

Agradezco cualquier ayuda.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

var searchResults = []; //this array will be filled with results that match the searched key string function search(jsonObject, key) { for (let k in jsonObject) { let innerObject = jsonObject[k]; if (k == key) { searchResults.push(innerObject); } //recursively search for arrays and objects deep inside: if (innerObject instanceof Array || typeof innerObject == "object") { search(innerObject, key); } } } search(machine, "sku"); //searchResults will be: [ "qweert-12", "qwer-12", "qwer-14", "qmqert-12", "qwlr-12", "qwuer-14" ]

Uso de una función recursiva para buscar niveles anidados. Espero tener la función que estabas buscando.

about 4 years ago · Santiago Trujillo 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!