嘗試執行 XMLHttprequest 來操作 JSON 并將其保存在我的本地驅動器上。
這是代碼:
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);
}
我的 JSON 檔案:
[
{
"state": "geschlossen",
"number": 1,
"class": "A",
"price": 10
},
{
"state": "geschlossen",
"number": 2,
"class": "B",
"price": 20
},
{
"state": "geschlossen",
"number": 3,
"class": "C",
"price": 30
}
]
如果我輸入值 10,20 或 30 但我希望每次索引 -1 都無關緊要時,findIndex 都會給我:
price : 10 -> index 0 ; price : 20 -> index 1 ; price : 30 -> index 2 ;
那么findIndex的問題在哪里呢?
uj5u.com熱心網友回復:
看起來您必須使用obj.price代替obj.name此處:
var index = jsonArr.findIndex(obj => obj.name===price);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365274.html
標籤:javascript json 阿贾克斯
