js 根据id 取出数组中对应的对象

896 阅读1分钟

js 根据id 取出数组中对应的对象

var id=1;
var obj=this.studentL.find(function (obj) {
      return obj.studentId ==id;//取出this.studentL里的id为1的那条数据所有的信息
    }
console.log(obj);

2.如果id是一个数组,数组中有多个值,根据数组中的值,筛选出id为数组中值的对象数据(Vue中用到的)

 定义方法:

 myFilter(arr1,arr2) {
    return  arr1.filter((ele) => 
        arr2.filter((x) => x === ele.studentId).length > 0
    );
  }

调用:

 var checkedS= this.myFilter(this.studentL,c);
alert("qqqqqq====>"+JSON.stringify(checkedS));//学生id 打印数据

转自:blog.csdn.net/weixin_4147…