Javascript 删除条目或将JSON文件更新为空白(即“空”)
我试图简单地创建一个删除函数,以简单地删除JSON文件中找到的条目 到目前为止,下面的代码成功地找到了我想要删除的行(即条目),或者用空白的Javascript 删除条目或将JSON文件更新为空白(即“空”),javascript,json,fetch,Javascript,Json,Fetch,我试图简单地创建一个删除函数,以简单地删除JSON文件中找到的条目 到目前为止,下面的代码成功地找到了我想要删除的行(即条目),或者用空白的“”进行更新-但是我很难将其写回已删除的JSON文件。。。这是抓取刚刚好,我找到了线,我想删除刚刚好 // Delete Function dele.addEventListener('click', ()=>{ let e= document.getElementById("dropdown-of-entry"); let s
“
”进行更新-但是我很难将其写回已删除的JSON文件。。。这是抓取刚刚好,我找到了线,我想删除刚刚好
// Delete Function
dele.addEventListener('click', ()=>{
let e= document.getElementById("dropdown-of-entry");
let slctn= e.value;
console.log(slctn);
fetch(urld)
.then(
function(response) {
response.json().then(function(data) {
let gData = JSON.stringify(data)
let jsonF = JSON.parse(gData);
let findtempto;
let jsonUpdt;
let jsonUpdtd;
for (let i = 0; i < jsonF.length; i++) {
findtempto = jsonF[i].styleName;
if (findtempto === slctn) {
console.log(jsonF[i]); // This successfully finds the line I want to delete
//delete jsonF[i]; // No avail
jsonF[i] = " "; // Am trying this next
// I have also tried moving the ajax call here, and lines above it
}
}
});
jsonUpdt = JSON.stringify(jsonF);
console.log(jsonUpdt);
jsonUpdtd = JSON.parse(jsonUpdt);
console.log(jsonUpdtd);
$.ajax({
url: './php/data/processor.php',
type: 'POST',
data: { template: jsonUpdtd },
success: function(msg) {
console.log('updated/deleted data');
}
});
}
)
.catch(function(err) {
console.error('Fetch Error -', err);
});
});
我认为这是一个JavaScript问题,我在这里处理不当,因为我正在使用./processor.php
将新条目写入JSON文件
// Delete Function
dele.addEventListener('click', ()=>{
let e= document.getElementById("dropdown-of-entry");
let slctn= e.value;
console.log(slctn);
fetch(urld)
.then(
function(response) {
response.json().then(function(data) {
let gData = JSON.stringify(data)
let jsonF = JSON.parse(gData);
let findtempto;
let jsonUpdt;
let jsonUpdtd;
for (let i = 0; i < jsonF.length; i++) {
findtempto = jsonF[i].styleName;
if (findtempto === slctn) {
console.log(jsonF[i]); // This successfully finds the line I want to delete
//delete jsonF[i]; // No avail
jsonF[i] = " "; // Am trying this next
// I have also tried moving the ajax call here, and lines above it
}
}
});
jsonUpdt = JSON.stringify(jsonF);
console.log(jsonUpdt);
jsonUpdtd = JSON.parse(jsonUpdt);
console.log(jsonUpdtd);
$.ajax({
url: './php/data/processor.php',
type: 'POST',
data: { template: jsonUpdtd },
success: function(msg) {
console.log('updated/deleted data');
}
});
}
)
.catch(function(err) {
console.error('Fetch Error -', err);
});
});
//删除函数
dele.addEventListener('单击',()=>{
设e=document.getElementById(“条目下拉列表”);
设slctn=e.value;
控制台日志(slctn);
获取(urld)
.那么(
功能(响应){
response.json().then(函数(数据){
让gData=JSON.stringify(数据)
让jsonF=JSON.parse(gData);
让我们找到空的;
让jsonUpdt;
让jsonUpdtd;
for(设i=0;i
使用array.filter()
将符合条件的元素保留在数组中
jsonF = jsonF.filter(e => e.styleName != slctn);
用类似于
“
的字符串替换对象将在以后引起问题,因为代码的其余部分希望数组包含对象。如果jsonF
应该是一个对象数组,则将字符串分配给jsonF[i]
可能会让JSON的其他用户感到困惑。不需要先字符串化然后再解析。我也尝试过不使用字符串化和解析。这与问题无关。