当前位置:网站首页>js对JSON数组的增删改查

js对JSON数组的增删改查

2022-07-06 15:41:00 ~疆

目录

查询记录。根据username查询记录

 删除记录。根据username删除记录

 修改记录。根据username修改password

新增记录。 


参考:JavaScript(js)中数组Array的常用方法_~疆的博客-CSDN博客_array jsArray.some()var arr=[1, 2, 3, 4, 5]var arr1=arr.some(x => x > 3);console.log(arr) //Array [1, 2, 3, 4, 5]console.log(arr1) //truesome() 方法用于检测数组中的元素是否满足指定条件(函数提供)。some() 方法会依次执行数组的每个元......https://blog.csdn.net/qq_40323256/article/details/103008280 

查询记录。根据username查询记录

    const users = [
      { username: "张三", password: 333 },
      { username: "李四", password: 444 },
      { username: "王五", password: 555 },
    ];

    //查询记录。根据username查询记录
    let data = users.filter((item) => item.username == "张三");
    console.log("根据username查询出的记录:", data);

 删除记录。根据username删除记录

    const users = [
      { username: "张三", password: 333 },
      { username: "李四", password: 444 },
      { username: "王五", password: 555 },
    ];

    //删除记录。根据username删除记录
    let data = users.filter((item) => item.username == "张三");
    console.log("根据username查询出的记录:", data);
    let index = users.indexOf(data[0]);
    console.log("索引index:", index);
    index !== -1 && users.splice(index, 1);
    console.log("删除一条记录后的users:", users);

 修改记录。根据username修改password

    const users = [
      { username: "张三", password: 333 },
      { username: "李四", password: 444 },
      { username: "王五", password: 555 },
    ];

    //修改记录。根据username修改password
    let data = users.filter((item) => item.username == "张三");
    console.log("根据username查询出的记录:", data);
    data[0].password = 3333;
    console.log("修改一条记录后的users:", users);

新增记录。 

 

    const users = [
      { username: "张三", password: 333 },
      { username: "李四", password: 444 },
      { username: "王五", password: 555 },
    ];

    //新增记录。
    users.push({ username: "赵六", password: 666 });
    console.log("新增一条记录后的users:", users);

原网站

版权声明
本文为[~疆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40323256/article/details/125630282