如何删除嵌入在另一个 JSON 数组 (f7Contacts) 中的特定 JSON 数组 (consultas),两者都保存在单个 localSorage 键中? [英] How can I delete a specific JSON array (consultas) embedded within another JSON array (f7Contacts) both saved in a single localSorage key?

查看:22
本文介绍了如何删除嵌入在另一个 JSON 数组 (f7Contacts) 中的特定 JSON 数组 (consultas),两者都保存在单个 localSorage 键中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储为 JSON 数组的 localStorage 键 (f7Contacts),这应该是.我知道如何使用 localStorage.removeItem("myKey"); 删除整个密钥.

我的数组,如您所见,由一组嵌入其中的 JSON 数组组成.有一个名为 f7Contacts 的更广泛的数组,其中有一些子数组(以某种方式命名)名为 visitingsconsultas.现在,我要删除的不是任何 JSON 数组中的单个项目,也不是整个 f7Contacts 数组,而是名为 consultas 的 JSON 子数组.

我尝试了很多方法,但我无法获得有效的代码.

这是我到目前为止所拥有的:

我的字符串化数组如下所示:

(现在我更新了字符串以使其更像我的真实案例,因为建议的答案设法删除了其中一个联系人中的 consultas,并且在一种情况下它删除了 consultascode>consultas 从第一个联系人并删除其余的联系人.我命名为 ELISA 和 FRANK 以便您区分它们.还更新了我的 Javascript 和 html 作为我自己的代码和 ЖнецЪ 的答案的混合物.我这样做是为了保持我的环境一致.这段代码设法删除了第一个 联系人咨询,但也删除了其余的联系人.):

JAVASCRIPT

 document.addEventListener(DOMContentLoaded", function() {var 联系人 = [{id:95470e9e-ee5c-4467-9500-1fcbeae1b57c",hasPic:真实,picId: 3,createdOn: "2021-08-01T17:31:32.230Z",名字:ELISA",公司:La Fábrica",工作:巴伦德罗",手机:+53555555",电话:+5372222222",短信:",电子邮件:fulano@nauta.cu",madedate: "无效日期",诊所:2",性别:M",生日:无效日期",年龄:47",访问:[{id:1",vdate:19/07/-2006 12:00 AM",pdcb:Est offcia dolorem",vdiagnose: "Laborum Ea consecte",vrecom:Mollit cillum sed pe",nextv: 无效日期"}],咨询:[{id:1",日期:04/07/-1978 12:00 AM",lmotive:Soluta cumque aspern",ldiagnose:Quidem autem dout o",测试:Rerum sint atque il",etest: "Officiis deserunt un",mtest:Nihil a dolore rem s",nextv: 无效日期"},{id:2",日期:04/04/-1991 12:00 AM",lmotive:Eius in est enim no",ldiagnose:In aliqua Sunt dol",测试:Sunt dolor eu sed N",etest:Quia aut reprehender",mtest:Unde libero autem an",nextv: 无效日期"}],isFavorite: 真},{id:95470e9e-ee5c-4467-9500-1fcbeae1b57c",hasPic:真实,picId: 3,createdOn: "2021-08-01T17:31:32.230Z",名字:弗兰克",公司:La Fábrica",工作:巴伦德罗",手机:+53555555",电话:+5372222222",短信:",电子邮件:fulano@nauta.cu",madedate: "无效日期",诊所:2",性别:M",生日:无效日期",年龄:47",访问:[{id:1",vdate:19/07/-2006 12:00 AM",pdcb:Est offcia dolorem",vdiagnose: "Laborum Ea consecte",vrecom:Mollit cillum sed pe",nextv: 无效日期"}],咨询:[{id:1",日期:04/07/-1978 12:00 AM",lmotive:Soluta cumque aspern",ldiagnose:Quidem autem dout o",测试:Rerum sint atque il",etest: "Officiis deserunt un",mtest:Nihil a dolore rem s",nextv: 无效日期"},{id:2",日期:04/04/-1991 12:00 AM",lmotive:Eius in est enim no",ldiagnose:In aliqua Sunt dol",测试:Sunt dolor eu sed N",etest:Quia aut reprehender",mtest:Unde libero autem an",nextv: 无效日期"}],isFavorite: 真}];//从 lS 获取const lS = JSON.parse(localStorage.getItem('f7Contacts'));//DOM 元素const cancelBtn = document.getElementById('dntdelete');const deleteBtn = document.getElementById('delete');const Consultas = document.getElementById('consultas');const json = document.getElementById('json');函数 clearConsults(){document.getElementById('confirm').style.display="block";{函数 removeItem() {const lS = JSON.parse(localStorage.getItem('f7Contacts'));如果(!lS)返回;const obj = lS[0];//在对象中找到'consultas'键const findKey = Object.keys(obj).find(key => key.includes('consultas'));如果(查找键){//从带有数组的对象中删除键删除 obj[findKey];}//将 obj 包装回数组 &放到localStoragelocalStorage.setItem('f7Contacts', JSON.stringify([obj]));//将item重新放入存储中}alert('CONSULTAS ELIMINADAS');document.getElementById('confirm').style.display="none";如果(lS[0].consultas){Consultas.textContent = lS[0].consultas.length;}咨询.textContent = 0;json.textContent = JSON.stringify(obj, undefined, 2);}cancelBtn.addEventListener('click', dntdelete);deleteBtn.addEventListener('click', clearConsults);}});document.getElementById('dntdelete').onclick = function(){alert('OPERACIÓN CANCELADA');document.getElementById('confirm').style.display="none";返回假;};

HTML

<div class="results"><h4>Consultas<span id=consultas"></span></h4>

<div class="control-buttons"><div id="确认";样式=显示:无"><button id="delete">Delete</button><button id="dntdelete">取消</button>

<a onclick="clearConsults()">删除咨询</a><h3>确定要删除吗?</h3>

</main>

我必须明确指出,这个 localStorage 键 (f7Contacts) 实际上是由应用程序中的另一个 js 存储的,如果我已经包含了 localStorage.setItem("f7Contacts", JSON.stringify(contacts)); 在我上面代码的开头只是为了让你清楚.还要明确字符串中有几个联系人,任务是删除所有consultas.

解决方案

如果我的理解是正确的,您只想从 localStorage 中删除 consultas.我认为最好用 Object.keys 找到对象的键,然后从对象中删除该键.工作示例

script.js

document.addEventListener('DOMContentLoaded', function () {var 联系人 = [{id: '95470e9e-ee5c-4467-9500-1fcbeae1b57c',hasPic:真实,picId: 3,createdOn: '2021-08-01T17:31:32.230Z',名字:'ELISA',公司:'La Fábrica',工作:'巴伦德罗',手机:'+53555555',电话:'+5372222222',短信: '',电子邮件:'fulano@nauta.cu',madedate: '无效日期',诊所:'2',性别:'M',生日:'无效日期',年龄:'47',来访:[{编号:'1',vdate: '19/07/-2006 12:00 AM',pdcb: 'Est offcia dolorem',vdiagnose: 'Laborum Ea consecte',vrecom: 'Mollit cillum sed pe',nextv: '无效日期',},],咨询:[{编号:'1',ldate: '04/07/-1978 12:00 AM',lmotive: 'Soluta cumque aspern',ldiagnose: 'Quidem autem do ut o',ttest: 'Rerum sint atque il',etest: 'Officiis deserunt un',mtest: 'Nihil a dolore rem s',nextv: '无效日期',},{编号:'2',ldate: '04/04/-1991 12:00 AM',lmotive: 'Eius in est enim no',ldiagnose: '在 aliqua Sunt dol',ttest: 'Sunt dolor eu sed N',etest: 'Quia aut reprehender',mtest: 'Unde libero autem an',nextv: '无效日期',},],isFavorite: 真,},{id: '95470e9e-ee5c-4467-9500-1fcbeae1b57c',hasPic:真实,picId: 3,createdOn: '2021-08-01T17:31:32.230Z',名字:'弗兰克',公司:'La Fábrica',工作:'巴伦德罗',手机:'+53555555',电话:'+5372222222',短信: '',电子邮件:'fulano@nauta.cu',madedate: '无效日期',诊所:'2',性别:'M',生日:'无效日期',年龄:'47',来访:[{编号:'1',vdate: '19/07/-2006 12:00 AM',pdcb: 'Est offcia dolorem',vdiagnose: 'Laborum Ea consecte',vrecom: 'Mollit cillum sed pe',nextv: '无效日期',},],咨询:[{编号:'1',ldate: '04/07/-1978 12:00 AM',lmotive: 'Soluta cumque aspern',ldiagnose: 'Quidem autem do ut o',ttest: 'Rerum sint atque il',etest: 'Officiis deserunt un',mtest: 'Nihil a dolore rem s',nextv: '无效日期',},{编号:'2',ldate: '04/04/-1991 12:00 AM',lmotive: 'Eius in est enim no',ldiagnose: '在 aliqua Sunt dol',ttest: 'Sunt dolor eu sed N',etest: 'Quia aut reprehender',mtest: 'Unde libero autem an',nextv: '无效日期',},],isFavorite: 真,},];//从 lS 获取const lS = JSON.parse(localStorage.getItem('f7Contacts'));//DOM 元素const clearConsultsBtn = document.getElementById('clear-consults');const confirmBtn = document.getElementById('confirm');const createBtn = document.getElementById('create');const cancelBtn = document.getElementById('dntdelete');const deleteBtn = document.getElementById('delete');const Consultas = document.getElementById('consultas');const controlBtns = document.querySelector('.control-buttons');const json = document.getElementById('json');//检查是否不是 lS如果(!lS){localStorage.setItem('f7Contacts', JSON.stringify(contacts));///只是为了能见度让 num = 0;for (const i of contact) {num += i.consultas.length;咨询.textContent = num;}json.textContent = JSON.stringify(contacts, undefined, 2);}如果 (lS) {for (const i of lS) {如果(!i.consultas)consultas.textContent = 0;}json.textContent = JSON.stringify(lS, undefined, 2);}函数 removeItem() {const lS = JSON.parse(localStorage.getItem('f7Contacts'));如果(!lS)返回;//创建没有 'consultas' 的新数组const clearArray = lS.map(obj => {//在对象中找到'consultas'键const findKey = Object.keys(obj).find(key => key.includes('consultas'));如果(查找键){//从带有数组的对象中删除键删除 obj[findKey];}返回对象;//将对象返回到数组而不使用 'consultas'});//放入本地存储localStorage.setItem('f7Contacts', JSON.stringify(clearArray));//将item重新放入存储中alert('CONSULTAS ELIMINADAS');controlBtns.style.display = '无';clearConsultsBtn.style.display = 'block';///只是为了能见度如果(清除数组){for (const i of clearArray) {如果(!i.consultas)consultas.textContent = 0;}}json.textContent = JSON.stringify(clearArray, undefined, 2);}函数 addItem() {localStorage.setItem('f7Contacts', JSON.stringify(contacts));///只是为了能见度让 num = 0;for (const i of contact) {num += i.consultas.length;咨询.textContent = num;}json.textContent = JSON.stringify(contacts, undefined, 2);}函数 clearConsults() {alert('OPERACIÓN CANCELADA');controlBtns.style.display = 'flex';clearConsultsBtn.style.display = '无';}函数取消(){alert('OPERACIÓN CANCELADA');controlBtns.style.display = '无';clearConsultsBtn.style.display = 'block';}clearConsultsBtn.addEventListener('click', clearConsults);createBtn.addEventListener('click', addItem);cancelBtn.addEventListener('点击', 取消);deleteBtn.addEventListener('click', removeItem);ConfirmBtn.addEventListener('click', clearConsults);});

index.html

<div class="results"><h4>Consultas数组<span id=consultas"></span></h4>

<h3>确定要删除吗?</h3><button id="clear-consults">Delete Consultas</button><div class="control-buttons"><button id="dntdelete">取消</button><button id="delete">Delete</button><button id="create">Create</button>

<div id="confirm"></div><pre id="json"></pre></main>

style.css

* {边距:0;填充:0;box-sizing: 边框框;字体系列:衬线;}身体,html {填充:20px;}主要的 {最小宽度:100%;显示:网格;网格模板行:重复(4,最大内容);间隙:10px;对齐内容:居中;颜色:#4c5f6b;}.控制按钮{显示:无;间隙:20px;}按钮 {填充:8px 15px;边界:无;颜色:#4c5f6b;边框半径:3px;光标:指针;}#取消 {背景颜色:#4c5f6b;}#删除 {背景颜色:#89608e;颜色:白烟;}#创建 {背景色:#6eaa8d;颜色:白烟;}.结果 {显示:弹性;间隙:20px;}.结果 h4 {文本转换:小写;}.结果跨度{宽度:50px;左边距:5px;填充:3px 9px;边框:2px 实心 #6eaa8d;}#确认 {显示:无;}预{字体大小:10px;}

I have a localStorage key (f7Contacts) stored as a JSON array, as it should be. I know how to delete the whole key using localStorage.removeItem("myKey");.

My array, as you can see is formed by a set of JSON arrays embeeded inside of it. There is the wider one named f7Contacts and inside of it there are some subarrays (to name them someway) named visitings and consultas. Now, what I'm trying to delete is not a single item from any of the JSON arrays nor the whole f7Contacts array but the JSON sub array named consultas.

I have tried so many ways but I could not get a working code.

This is what I've got until now:

My stringify array looks like this:

(now I have up dated the string to make it more alike my real case because the proposed answers manage to delete the consultas just in one of the contacts, and in a case it deletes the consultas from the first contact and deletes the rest of the contacts. I named ELISA and FRANK for you to differetiate them. Also updated my Javascript and html as a mixture from my own code and the one of ЖнецЪ's answer. I did this to keep my enviroment consistent. This code manage to delete the consultas of the first contact but also deletes the rest of the contacts.):

JAVASCRIPT

    document.addEventListener("DOMContentLoaded", function() {
    var contacts = [{
     id: "95470e9e-ee5c-4467-9500-1fcbeae1b57c",
     hasPic: true,
     picId: 3,
     createdOn: "2021-08-01T17:31:32.230Z",
     firstName: "ELISA",
     company: "La Fábrica",
     job: "Barrendero",
     mobile: "+53555555",
     phone: "+5372222222",
     sms: "",
     email: "fulano@nauta.cu",
     madedate: "Invalid date",
     clinic: "2",
     gender: "M",
     birthday: "Invalid date",
     age: "47",
     visitings: [{
        id: "1",
        vdate: "19/07/-2006 12:00 AM",
        pdcb: "Est officia dolorem ",
        vdiagnose: "Laborum Ea consecte",
        vrecom: "Mollit cillum sed pe",
        nextv: "Invalid date"
     }],

     consultas: [{
          id: "1",
          ldate: "04/07/-1978 12:00 AM",
          lmotive: "Soluta cumque aspern",
          ldiagnose: "Quidem autem do ut o",
          ttest: "Rerum sint atque il",
          etest: "Officiis deserunt un",
          mtest: "Nihil a dolore rem s",
          nextv: "Invalid date"
        },

        {
          id: "2",
          ldate: "04/04/-1991 12:00 AM",
          lmotive: "Eius in est enim no",
          ldiagnose: "In aliqua Sunt dol",
          ttest: "Sunt dolor eu sed N",
          etest: "Quia aut reprehender",
          mtest: "Unde libero autem an",
          nextv: "Invalid date"
        }
     ],
     isFavorite: true
    },
    {id: "95470e9e-ee5c-4467-9500-1fcbeae1b57c",
     hasPic: true,
     picId: 3,
     createdOn: "2021-08-01T17:31:32.230Z",
     firstName: "FRANK",
     company: "La Fábrica",
     job: "Barrendero",
     mobile: "+53555555",
     phone: "+5372222222",
     sms: "",
     email: "fulano@nauta.cu",
     madedate: "Invalid date",
     clinic: "2",
     gender: "M",
     birthday: "Invalid date",
     age: "47",
     visitings: [{
        id: "1",
        vdate: "19/07/-2006 12:00 AM",
        pdcb: "Est officia dolorem ",
        vdiagnose: "Laborum Ea consecte",
        vrecom: "Mollit cillum sed pe",
        nextv: "Invalid date"
    }],

     consultas: [{
          id: "1",
          ldate: "04/07/-1978 12:00 AM",
          lmotive: "Soluta cumque aspern",
          ldiagnose: "Quidem autem do ut o",
          ttest: "Rerum sint atque il",
          etest: "Officiis deserunt un",
          mtest: "Nihil a dolore rem s",
          nextv: "Invalid date"
        },

        {
          id: "2",
          ldate: "04/04/-1991 12:00 AM",
          lmotive: "Eius in est enim no",
          ldiagnose: "In aliqua Sunt dol",
          ttest: "Sunt dolor eu sed N",
          etest: "Quia aut reprehender",
          mtest: "Unde libero autem an",
          nextv: "Invalid date"
        }
    ],

    isFavorite: true
    }];

  // Get from lS
    const lS = JSON.parse(localStorage.getItem('f7Contacts'));
    // DOM elements
    const cancelBtn = document.getElementById('dntdelete');
    const deleteBtn = document.getElementById('delete');
    const consultas = document.getElementById('consultas');
    const json = document.getElementById('json');


    function clearConsults(){
    document.getElementById('confirm').style.display="block";
    {
    function removeItem() {
    const lS = JSON.parse(localStorage.getItem('f7Contacts'));
    if (!lS) return;

    const obj = lS[0];
    // Find 'consultas' key in the object
    const findKey = Object.keys(obj).find(key => key.includes('consultas'));
    if (findKey) {
      // Delete key from the object with array
      delete obj[findKey];
    }
    // Back wraping the obj into array & put to the localStorage
    localStorage.setItem('f7Contacts', JSON.stringify([obj])); //set item back into storage
    }
    alert('CONSULTAS ELIMINADAS');
    document.getElementById('confirm').style.display="none";

    if (lS[0].consultas) {
    consultas.textContent = lS[0].consultas.length;
    }
    consultas.textContent = 0;
    json.textContent = JSON.stringify(obj, undefined, 2);
    }

    cancelBtn.addEventListener('click', dntdelete);
    deleteBtn.addEventListener('click', clearConsults);
    }
    });

    document.getElementById('dntdelete').onclick = function(){
    alert('OPERACIÓN CANCELADA');
    document.getElementById('confirm').style.display="none";
    return false;
    };

HTML

<main>
<div class="results">
<h4>Consultas<span id="consultas"></span></h4>
</div>
<div class="control-buttons">
<div id="confirm" style="display: none">
<button id="delete">Delete</button>
<button id="dntdelete">Cancel</button>
</div>
<a onclick="clearConsults()">Delete Consultas</a><h3>SURE YOU WANT TO DELETE?</h3>
</div>
</main>
    

I must let it clear that this localStorage key (f7Contacts) is really stored by another js within the app and if I have included the localStorage.setItem("f7Contacts", JSON.stringify(contacts)); at the beginning of my code above is just as an attempt to make it clear for you. And let clear too that there are several contacts within the string and the task is to delete consultas for them all.

解决方案

If i'm right understood you want removed only consultas from the localStorage. I think better to find the key of the object with Object.keys and then delete that key from the object. working example

script.js

document.addEventListener('DOMContentLoaded', function () {
  var contacts = [
    {
      id: '95470e9e-ee5c-4467-9500-1fcbeae1b57c',
      hasPic: true,
      picId: 3,
      createdOn: '2021-08-01T17:31:32.230Z',
      firstName: 'ELISA',
      company: 'La Fábrica',
      job: 'Barrendero',
      mobile: '+53555555',
      phone: '+5372222222',
      sms: '',
      email: 'fulano@nauta.cu',
      madedate: 'Invalid date',
      clinic: '2',
      gender: 'M',
      birthday: 'Invalid date',
      age: '47',
      visitings: [
        {
          id: '1',
          vdate: '19/07/-2006 12:00 AM',
          pdcb: 'Est officia dolorem ',
          vdiagnose: 'Laborum Ea consecte',
          vrecom: 'Mollit cillum sed pe',
          nextv: 'Invalid date',
        },
      ],

      consultas: [
        {
          id: '1',
          ldate: '04/07/-1978 12:00 AM',
          lmotive: 'Soluta cumque aspern',
          ldiagnose: 'Quidem autem do ut o',
          ttest: 'Rerum sint atque il',
          etest: 'Officiis deserunt un',
          mtest: 'Nihil a dolore rem s',
          nextv: 'Invalid date',
        },

        {
          id: '2',
          ldate: '04/04/-1991 12:00 AM',
          lmotive: 'Eius in est enim no',
          ldiagnose: 'In aliqua Sunt dol',
          ttest: 'Sunt dolor eu sed N',
          etest: 'Quia aut reprehender',
          mtest: 'Unde libero autem an',
          nextv: 'Invalid date',
        },
      ],
      isFavorite: true,
    },
    {
      id: '95470e9e-ee5c-4467-9500-1fcbeae1b57c',
      hasPic: true,
      picId: 3,
      createdOn: '2021-08-01T17:31:32.230Z',
      firstName: 'FRANK',
      company: 'La Fábrica',
      job: 'Barrendero',
      mobile: '+53555555',
      phone: '+5372222222',
      sms: '',
      email: 'fulano@nauta.cu',
      madedate: 'Invalid date',
      clinic: '2',
      gender: 'M',
      birthday: 'Invalid date',
      age: '47',
      visitings: [
        {
          id: '1',
          vdate: '19/07/-2006 12:00 AM',
          pdcb: 'Est officia dolorem ',
          vdiagnose: 'Laborum Ea consecte',
          vrecom: 'Mollit cillum sed pe',
          nextv: 'Invalid date',
        },
      ],

      consultas: [
        {
          id: '1',
          ldate: '04/07/-1978 12:00 AM',
          lmotive: 'Soluta cumque aspern',
          ldiagnose: 'Quidem autem do ut o',
          ttest: 'Rerum sint atque il',
          etest: 'Officiis deserunt un',
          mtest: 'Nihil a dolore rem s',
          nextv: 'Invalid date',
        },

        {
          id: '2',
          ldate: '04/04/-1991 12:00 AM',
          lmotive: 'Eius in est enim no',
          ldiagnose: 'In aliqua Sunt dol',
          ttest: 'Sunt dolor eu sed N',
          etest: 'Quia aut reprehender',
          mtest: 'Unde libero autem an',
          nextv: 'Invalid date',
        },
      ],
      isFavorite: true,
    },
  ];

  // Get from lS
  const lS = JSON.parse(localStorage.getItem('f7Contacts'));
  // DOM elements
  const clearConsultsBtn = document.getElementById('clear-consults');
  const confirmBtn = document.getElementById('confirm');
  const createBtn = document.getElementById('create');
  const cancelBtn = document.getElementById('dntdelete');
  const deleteBtn = document.getElementById('delete');
  const consultas = document.getElementById('consultas');
  const controlBtns = document.querySelector('.control-buttons');
  const json = document.getElementById('json');
  // Check if not lS
  if (!lS) {
    localStorage.setItem('f7Contacts', JSON.stringify(contacts));

    /// JUST FOR VISIBILITY
    let num = 0;
    for (const i of contacts) {
      num += i.consultas.length;
      consultas.textContent = num;
    }
    json.textContent = JSON.stringify(contacts, undefined, 2);
  }

  if (lS) {
    for (const i of lS) {
      if (!i.consultas) consultas.textContent = 0;
    }
    json.textContent = JSON.stringify(lS, undefined, 2);
  }

  function removeItem() {
    const lS = JSON.parse(localStorage.getItem('f7Contacts'));
    if (!lS) return;

    // Creating new array without 'consultas'
    const clearArray = lS.map(obj => {
      // Find 'consultas' key in the object
      const findKey = Object.keys(obj).find(key => key.includes('consultas'));
      if (findKey) {
        // Delete key from the object with array
        delete obj[findKey];
      }
      return obj; // Returning object to array without 'consultas'
    });
    // Put to the localStorage
    localStorage.setItem('f7Contacts', JSON.stringify(clearArray)); //set item back into storage
    alert('CONSULTAS ELIMINADAS');
    controlBtns.style.display = 'none';
    clearConsultsBtn.style.display = 'block';

    /// JUST FOR VISIBILITY
    if (clearArray) {
      for (const i of clearArray) {
        if (!i.consultas) consultas.textContent = 0;
      }
    }
    json.textContent = JSON.stringify(clearArray, undefined, 2);
  }

  function addItem() {
    localStorage.setItem('f7Contacts', JSON.stringify(contacts));

    /// JUST FOR VISIBILITY
    let num = 0;
    for (const i of contacts) {
      num += i.consultas.length;
      consultas.textContent = num;
    }
    json.textContent = JSON.stringify(contacts, undefined, 2);
  }

  function clearConsults() {
    alert('OPERACIÓN CANCELADA');
    controlBtns.style.display = 'flex';
    clearConsultsBtn.style.display = 'none';
  }

  function cancel() {
    alert('OPERACIÓN CANCELADA');
    controlBtns.style.display = 'none';
    clearConsultsBtn.style.display = 'block';
  }

  clearConsultsBtn.addEventListener('click', clearConsults);
  createBtn.addEventListener('click', addItem);
  cancelBtn.addEventListener('click', cancel);
  deleteBtn.addEventListener('click', removeItem);
  confirmBtn.addEventListener('click', clearConsults);
});

index.html

<main>
  <div class="results">
    <h4>Consultas arrays<span id="consultas"></span></h4>
  </div>
  <h3>SURE YOU WANT TO DELETE?</h3>
  <button id="clear-consults">Delete Consultas</button>
  <div class="control-buttons">
    <button id="dntdelete">Cancel</button>
    <button id="delete">Delete</button>
    <button id="create">Create</button>
  </div>
  <div id="confirm"></div>
  <pre id="json"></pre>
</main>

style.css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: serif;
}

body,
html {
  padding: 20px;
}

main {
  min-width: 100%;
  display: grid;
  grid-template-rows: repeat(4, max-content);
  gap: 10px;
  justify-content: center;
  color: #4c5f6b;
}
.control-buttons {
  display: none;
  gap: 20px;
}
button {
  padding: 8px 15px;
  border: none;
  color: #4c5f6b;

  border-radius: 3px;
  cursor: pointer;
}
#cancel {
  background-color: #4c5f6b;
}
#delete {
  background-color: #89608e;
  color: whitesmoke;
}
#create {
  background-color: #6eaa8d;
  color: whitesmoke;
}
.results {
  display: flex;
  gap: 20px;
}
.results h4 {
  text-transform: lowercase;
}
.results span {
  width: 50px;
  margin-left: 5px;
  padding: 3px 9px;
  border: 2px solid #6eaa8d;
}
#confirm {
  display: none;
}
pre {
  font-size: 10px;
}

这篇关于如何删除嵌入在另一个 JSON 数组 (f7Contacts) 中的特定 JSON 数组 (consultas),两者都保存在单个 localSorage 键中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆