Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Search inside a JSON Javascript

I have a problem concern search JSON string and i have JSON string

{"userDetail":[
 {
   "Name": "Scottic Mangry",
   "Age" : "12", 
 },
 {
   "Name": "Joneson Mangly",
   "Age" : "18", 
 },
 {
   "Name": "Saoyu Wang",
   "Age" : "15", 
 },
]}

And data search

let searchObj = "Mang"

I need a result

 {
   "Name": "Scottic Mangry",
   "Age" : "12", 
 },
 {
   "Name": "Joneson Mangly",
   "Age" : "18", 
 }

Any help or suggestions would be great!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Something like:

let data = {
    "userDetail":[
     {
       "Name": "Scottic Mangry",
       "Age" : "12", 
     },
     {
       "Name": "Joneson Mangly",
       "Age" : "18", 
     },
     {
       "Name": "Saoyu Wang",
       "Age" : "15", 
     },
    ]
}

let needle = "Mang";
let result = data['userDetail'].filter(el => el.Name.includes(needle));
console.log(result); 

needle is the string we are searching for inside the Name property of the data.userDetail object

includes is case sensitive, so if you want to match results regardless of case it's easiest to lowercase everything first

about 4 years ago · Juan Pablo Isaza Relatório

0

you can do it using Array.filter, Array.values and includes

const data = {"userDetail":[
 {
   "Name": "Scottic Mangry",
   "Age" : "12", 
 },
 {
   "Name": "Joneson Mangly",
   "Age" : "18", 
 },
 {
   "Name": "Saoyu Wang",
   "Age" : "15", 
 },
]}


const search = (data, search) => data.filter(d => Object.values(d).some(v => v.includes(search)))

console.log(search(data.userDetail, "Mang"))

about 4 years ago · Juan Pablo Isaza Relatório

0

As cmgchess say in comments Using filter and includes would do the job for you: to make it more declarative and readable I put it inside a function called findName

const objs = {
  "userDetail": [{
      "Name": "Scottic Mangry",
      "Age": "12",
    },
    {
      "Name": "Joneson Mangly",
      "Age": "18",
    },
    {
      "Name": "Saoyu Wang",
      "Age": "15",
    },
  ]
}

function findName(name) {
  return objs.userDetail.filter(user => user.Name.includes(name))
}
let searchObj = "Mang";
console.log(findName(searchObj));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda