在对象数组中检索属性为指定值得某个对象使用map()就非常方便。
对象数组
var studentArray = [ {"name":"小明","gender":"男","subject":[‘语文‘,‘数学‘],"score":85}, {"name":"小刚","gender":"男","subject":[‘英语‘,‘数学‘],"score":90}, {"name":"小红","gender":"女","subject":[‘语文‘,‘英语‘],"score":72}, {"name":"小方","gender":"女","subject":[‘语文‘,‘历史‘],"score":63}, {"name":"小王","gender":"男","subject":[‘物理‘,‘化学‘],"score":89} ];
检索name为“小刚”的学生
var MyIndex = studentArray.map((item) => item.name).indexOf("小刚");
通过map()将原对象数组以name属性为元素重组为一个新的数组。
原文地址:https://www.cnblogs.com/MirageFox/p/9382285.html
时间: 2024-10-18 08:24:43