对json对象按照id精确查询并修改值
来源:互联网 发布:oracle数据库出现问题 编辑:程序博客网 时间:2024/04/30 07:24
用js的写法,只要找某个json,找到了以后,就break ,结束循环,这样性能最好。之前百度查到用jquery写,但是jquery是递归效率低,所以改为原生js,jquery版本的我没试,js版本得行。
jquery版本
function getAllCld(obj){ for(var o in obj){ if(typeof o == "string"){ //dosomething } else { getAllCld(o); } }}
js版本
var zNodes=[ { id:101, pId:0, name:"全部"}, { id:201, pId:101, name:"管理人员"}, { id:202, pId:101, name:"驻巡人员"}, { id:203, pId:101, name:"企业"}, { id:20488, pId:101, name:"管理处"}, { id:20101, pId:201, name:"忻府区道路运输管理所"}, { id:20102, pId:201, name:"定襄县道路运输管理所 "}, { id:20103, pId:201, name:"五台县道路运输管理所"}, { id:20104, pId:201, name:"原平市道路运输管理所"}, { id:20105, pId:201, name:"代县道路运输管理所"}, { id:20106, pId:201, name:"繁峙县道路运输管理所"}, { id:20107, pId:201, name:"宁武县道路运输管理所"}, { id:20108, pId:201, name:"静乐县道路运输管理所"}, { id:20109, pId:201, name:"神池县道路运输管理所"}, { id:20110, pId:201, name:"五寨县道路运输管理所"}, { id:20111, pId:201, name:"岢岚县道路运输管理所"}, { id:20112, pId:201, name:"河曲县道路运输管理所"}, { id:20113, pId:201, name:"保德县道路运输管理所"}, { id:20114, pId:201, name:"偏关县道路运输管理所"}, { id:20201, pId:202, name:"忻府区道路运输管理所"}, { id:20202, pId:202, name:"定襄县道路运输管理所 "}, { id:20203, pId:202, name:"五台县道路运输管理所"}, { id:20204, pId:202, name:"原平市道路运输管理所"}, { id:20205, pId:202, name:"代县道路运输管理所"}, { id:20206, pId:202, name:"繁峙县道路运输管理所"}, { id:20207, pId:202, name:"宁武县道路运输管理所"}, { id:20208, pId:202, name:"静乐县道路运输管理所"}, { id:20209, pId:202, name:"神池县道路运输管理所"}, { id:20210, pId:202, name:"五寨县道路运输管理所"}, { id:20211, pId:202, name:"岢岚县道路运输管理所"}, { id:20212, pId:202, name:"河曲县道路运输管理所"}, { id:20213, pId:202, name:"保德县道路运输管理所"}, { id:20214, pId:202, name:"偏关县道路运输管理所"}, { id:1, pId:203, name:"忻府区道路运输管理所"}, { id:2, pId:203, name:"定襄县道路运输管理所 "}, { id:3, pId:203, name:"五台县道路运输管理所"}, { id:4, pId:203, name:"原平市道路运输管理所"}, { id:5, pId:203, name:"代县道路运输管理所"}, { id:6, pId:203, name:"繁峙县道路运输管理所"}, { id:7, pId:203, name:"宁武县道路运输管理所"}, { id:8, pId:203, name:"静乐县道路运输管理所"}, { id:9, pId:203, name:"神池县道路运输管理所"}, { id:10, pId:203, name:"五寨县道路运输管理所"}, { id:11, pId:203, name:"岢岚县道路运输管理所"}, { id:12, pId:203, name:"河曲县道路运输管理所"}, { id:13, pId:203, name:"保德县道路运输管理所"}, { id:14, pId:203, name:"偏关县道路运输管理所"}, ]; for(var item in zNodes){ if(zNodes[item].id==12){ zNodes[item].pId=303; zNodes[item].checked="true"; break; } } console.log(zNodes);
结果如下
0:{id: 101, pId: 0, name: "全部"}1:{id: 201, pId: 101, name: "管理人员"}2:{id: 202, pId: 101, name: "驻巡人员"}3:{id: 203, pId: 101, name: "企业"}4:{id: 20488, pId: 101, name: "管理处"}5:{id: 20101, pId: 201, name: "忻府区道路运输管理所"}6:{id: 20102, pId: 201, name: "定襄县道路运输管理所 "}7:{id: 20103, pId: 201, name: "五台县道路运输管理所"}8:{id: 20104, pId: 201, name: "原平市道路运输管理所"}9:{id: 20105, pId: 201, name: "代县道路运输管理所"}10:{id: 20106, pId: 201, name: "繁峙县道路运输管理所"}11:{id: 20107, pId: 201, name: "宁武县道路运输管理所"}12:{id: 20108, pId: 201, name: "静乐县道路运输管理所"}13:{id: 20109, pId: 201, name: "神池县道路运输管理所"}14:{id: 20110, pId: 201, name: "五寨县道路运输管理所"}15:{id: 20111, pId: 201, name: "岢岚县道路运输管理所"}16:{id: 20112, pId: 201, name: "河曲县道路运输管理所"}17:{id: 20113, pId: 201, name: "保德县道路运输管理所"}18:{id: 20114, pId: 201, name: "偏关县道路运输管理所"}19:{id: 20201, pId: 202, name: "忻府区道路运输管理所"}20:{id: 20202, pId: 202, name: "定襄县道路运输管理所 "}21:{id: 20203, pId: 202, name: "五台县道路运输管理所"}22:{id: 20204, pId: 202, name: "原平市道路运输管理所"}23:{id: 20205, pId: 202, name: "代县道路运输管理所"}24:{id: 20206, pId: 202, name: "繁峙县道路运输管理所"}25:{id: 20207, pId: 202, name: "宁武县道路运输管理所"}26:{id: 20208, pId: 202, name: "静乐县道路运输管理所"}27:{id: 20209, pId: 202, name: "神池县道路运输管理所"}28:{id: 20210, pId: 202, name: "五寨县道路运输管理所"}29:{id: 20211, pId: 202, name: "岢岚县道路运输管理所"}30:{id: 20212, pId: 202, name: "河曲县道路运输管理所"}31:{id: 20213, pId: 202, name: "保德县道路运输管理所"}32:{id: 20214, pId: 202, name: "偏关县道路运输管理所"}33:{id: 1, pId: 203, name: "忻府区道路运输管理所"}34:{id: 2, pId: 203, name: "定襄县道路运输管理所 "}35:{id: 3, pId: 203, name: "五台县道路运输管理所"}36:{id: 4, pId: 203, name: "原平市道路运输管理所"}37:{id: 5, pId: 203, name: "代县道路运输管理所"}38:{id: 6, pId: 203, name: "繁峙县道路运输管理所"}39:{id: 7, pId: 203, name: "宁武县道路运输管理所"}40:{id: 8, pId: 203, name: "静乐县道路运输管理所"}41:{id: 9, pId: 203, name: "神池县道路运输管理所"}42:{id: 10, pId: 203, name: "五寨县道路运输管理所"}43:{id: 11, pId: 203, name: "岢岚县道路运输管理所"}44:{id: 12, pId: 303, name: "河曲县道路运输管理所", checked: "true"}45:{id: 13, pId: 203, name: "保德县道路运输管理所"}46:{id: 14, pId: 203, name: "偏关县道路运输管理所"}length:47__proto__:Array(0)
阅读全文
0 0
- 对json对象按照id精确查询并修改值
- c#-WinForm-按照学号精确查询对象
- js对json对象排序,并删除id相同项
- javascript 根据给定的id集合查询对应的值,返回 json对象
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- mysql 查询并按照首字母排序
- Java读取json文件并对json数据进行读取、添加、删除与修改操作
- sql查询并修改
- 数据库只查询user表的id和name字段,并对name进行去重
- 用java面向对象的内容建立学生姓名,学号,id,班级,每一科的成绩,以及对管理员实现(对老师只实现查看):通过名字查询成绩,通过学号查询成绩,通过id修改姓名,通过姓名修改成绩
- 对数组元素是object对象,按照对象的key的值进行去重
- 用jQuery Post json对象、java端接收并还原为查询参数对象
- 利用dbcp数据库连接池连接数据库,并利用dbtuils框架对数据库test中book表进行插入、删除、修改、查询的操作。book表结构如下:(id,name,price,author,descrip
- 按照艺术家分组并输出某个艺术家的唯一键-值对
- Java json按照字符串顺序解析,并逆向读取
- js对Json数据按照某一字段排序的方法
- 根据经纬度查询地址并对获取的json数据进行解析
- 根据经纬度查询地址并对获取的json数据进行解析
- json
- 管理需求激增
- 7-15 两个有序链表序列的合并(20 分)
- eclipse用maven打包报错:http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionExcepti
- JVM内存分配模型
- 对json对象按照id精确查询并修改值
- 7-16 两个有序链表序列的交集(20 分)
- 最全的常用正则表达式大全
- Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 0. 准备工作
- java监听-持续完善中
- DDK编译驱动需要的文件 rc文件,makefile和SOURCE文件
- VMware“该虚拟机似乎正在使用中” 无法正常开机解决方案
- 宋宝华:火焰图:全局视野的Linux性能剖析
- Mac OS X 启用 Web 服务器