var persons = [ {name: "tina", age: 14}, {name: "timo", age: 15}, {name: "lily", age: 16}, {name: "lucy", age: 16} ]
2、根据对象属性值得到相应对象
//1. 获取 name 等于 lily 的对象 var lily = persons.filter((p) => { return p.name == "lily"; }); console.log(lily); //打印结果 [{name: "lily", age: 16}] //注:filter()方法返回的是一个数组 var twins = persons.filter((p) => { return p.age == 16; }); console.log(twins); //打印结果 [{name: "lily", age: 16},{name: "lucy", age: 16}]
3、删除其中一个对象
//删除 name 等于 tina 的对象,利用splice()方法 //1. 首先我们要得到这个对象 var tina = persons.filter((p) => { return p.name == "tina"; }); //2. 其次得到这个对象在数组中对应的索引 var index = persons.indexOf(tina[0]); //3. 如果存在则将其删除,index > -1 代表存在 index > -1 && persons.splice(index, 1); console.log(persons); //打印结果 [{name: "timo", age: 15}, {name: "lily", age: 16}, {name: "lucy", age: 16}]
4、修改其中一个对象的属性值
//将 name 等于 timo 的 age 修改为 20 //1. 得到 timo 对象 var timo = persons.filter((p) => { return p.name == "timo"; }); //2. 修改age timo[0].age = 20;
5、数组中添加一个对象
persons.push({name: "similar", age: 18});
标签:jqeury,name,16,age,改查,persons,json,lily,timo From: https://www.cnblogs.com/sishahu/p/16858257.html