Find values in JSON by multiple keys

1

I have the following JSON:

{
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

I need a function that looks for the key name and then look for a number inside it, and return the whole object. For example:

json("$MT", "91");

Expected return:

"$MT": {
            "0": "91",
            "1": "00:00:34.187",
            "2": "00:18:44.001\n"
        }

I have the following function:

var json = {}; 
function addJson(data){
   var chave; 
   for(var i in data){
      if(i == 0){ 
         chave = data[i]; 
         json[chave] = {}; 
      }else{
         json[chave][i-1] = data[i]; 
      }
   }
}

But it returns only a single key.

    
asked by anonymous 14.11.2018 / 23:01

3 answers

0

You can simply access the attribute by the first key and then use the to check if the value is contained within the object

const buscar = (dados, chave, conteudo) => {
  const item = dados[chave];
  
  return Object.values(item).indexOf(conteudo) !== -1 ? item : null;
};

// Teste
const json = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
};

console.log(buscar(json, '$SP', '92'));
console.log(buscar(json, '$MT', '91'));
  

Object.values()

     

The Object.values () method returns an array with the property values of a given object ...

    
20.11.2018 / 14:01
3

I did the following, according to what I understood the question, if it was something obscure comment on down here that I help you.

let meuJSON = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

const getJSON = (estado, conteudo, json) => {
  const jsonEmArray = Object.values(json[estado]) // Transforma os valores em Array
  let resultado = {}

  jsonEmArray.forEach(elemento => {
    if (elemento === conteudo) resultado = json[estado]
  })

  return resultado
}

let jsonFiltrado = getJSON('$SP', '92', meuJSON)

console.log(jsonFiltrado)
    
14.11.2018 / 23:40
0

@EduardoRibeiro works when I call getJSON only once, when I call for example

let jsonFiltrado = getJSON('$SP', '92', meuJSON)
let jsonFiltrado2 = getJSON('$MT', '91', meuJSON)

console.log(jsonFiltrado)
console.log(jsonFiltrado2)

does not return any of the two calls

let meuJSON = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

const getJSON = (estado, conteudo, json) => {
  const jsonEmArray = Object.values(json[estado]) // Transforma os valores em Array
  let resultado = {}

  jsonEmArray.forEach(elemento => {
    if (elemento === conteudo) resultado = json[estado]
  })

  return resultado
}

let jsonFiltrado = getJSON('$SP', '92', meuJSON)

console.log(jsonFiltrado)
    
20.11.2018 / 13:43