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

148
Visualizações
XMLHttpRequest manipulate JSON-File

trying to do a XMLHttprequest to manipulate a JSON and save it on my local drive.

Here is the code:

   
     function xml(){
        var xhr = new XMLHttpRequest(),
            jsonArr,
            method = "GET",
            jsonRequestURL = "win_lose.json";
        price = $('#price').val();
        xhr.open(method, jsonRequestURL, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                // we convert your JSON into JavaScript object
                jsonArr = JSON.parse(xhr.responseText);
                var index = jsonArr.findIndex(obj => obj.name===price);
                jsonArr.splice(index);
                console.log(price);
                console.log(index);
                xhr.open("POST", jsonRequestURL, true);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send("jsonTxt=" + JSON.stringify(jsonArr));
            }

        };
        xhr.send(null);
    }

My JSON-File:

[
   {
      "state": "geschlossen",
      "number": 1,
      "class": "A",
      "price": 10
    },
    {
      "state": "geschlossen",
      "number": 2,
      "class": "B",
      "price": 20
    },
    {
      "state": "geschlossen",
      "number": 3,
      "class": "C",
      "price": 30
    }
  ]

findIndex gives me the everytime the index -1 doesnt matter if i enter the value 10,20 or 30 but i expect : price : 10 -> index 0 ; price : 20 -> index 1 ; price : 30 -> index 2 ;

So where is the problem with the findIndex?

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

0

It looks like you have to use the obj.price instead of the obj.name here: var index = jsonArr.findIndex(obj => obj.name===price);

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