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

147
Views
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 answers
Answer question

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 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!