业务场景:
有两个数据源A和B,格式为数组包裹的对象列表,对象里面有标识字段“user_id”
infoA = [
{
user_id: 4,
org_id: 9,
org_no: "1212121",
name: "杨赛",
sex: "男",
cardno: "142729199611101418",
phone: "13522687472",
nation: "汉",
native: "江苏省南京市",
birthday: "-",
health: "其他",
marital_status: "其他",
classification: "未定义",
face: "其他",
education: "其他",
expand: "",
status: "其他",
is_delete: 0,
updated_at: "2021-12-01",
created_at: "2021-11-25",
org_name: "1212121",
},
{
user_id: 8,
org_id: 1,
org_no: "01",
name: "李歌女",
sex: "男",
cardno: "142719199711191257",
phone: "13522681234",
nation: "汉",
native: "江苏省南京市",
birthday: "-",
health: "其他",
marital_status: "其他",
classification: "未定义",
face: "其他",
education: "其他",
expand: "123",
status: "其他",
is_delete: 0,
updated_at: null,
created_at: "2021-11-26",
org_name: "连云区一中",
},
{
user_id: 9,
org_id: 1,
org_no: "01",
name: "黄春花",
sex: "男",
cardno: "321719199711191257",
phone: "13522685634",
nation: "汉",
native: "广东省珠海市",
birthday: "-",
health: "其他",
marital_status: "其他",
classification: "未定义",
face: "其他",
education: "其他",
expand: "fff",
status: "其他",
is_delete: 0,
updated_at: null,
created_at: "2021-12-29",
org_name: "连云区一中",
},
{
user_id: 11,
org_id: 1,
org_no: "01",
name: "李歌女2",
sex: "男",
cardno: "142719199711191258",
phone: "13522681234",
nation: "汉",
native: "江苏省南京市",
birthday: "-",
health: "其他",
marital_status: "其他",
classification: "未定义",
face: "其他",
education: "其他",
expand: "123",
status: "其他",
is_delete: 0,
updated_at: null,
created_at: null,
org_name: "连云区一中",
},
],
infoB = [
{
user_id: 4,
org_id: 9,
org_no: "1212121",
name: "杨赛",
sex: 1,
cardno: "142729199611101418",
phone: "13522687472",
nation: "汉",
native: "江苏省南京市",
birthday: "19961110",
health: 0,
marital_status: 0,
classification: 0,
face: 0,
education: 1,
expand: "",
status: 0,
is_delete: 0,
updated_at: "1638343691",
created_at: "1637823725",
},
{
user_id: 8,
org_id: 1,
org_no: "01",
name: "李歌女",
sex: 2,
cardno: "142719199711191257",
phone: "13522681234",
nation: "汉",
native: "江苏省南京市",
birthday: "19971119",
health: 1,
marital_status: 1,
classification: 1,
face: 1,
education: 1,
expand: "123",
status: 0,
is_delete: 0,
updated_at: null,
created_at: "1637893773",
},
{
user_id: 9,
org_id: 1,
org_no: "01",
name: "黄春花",
sex: 2,
cardno: "321719199711191257",
phone: "13522685634",
nation: "汉",
native: "广东省珠海市",
birthday: "19971119",
health: 1,
marital_status: 1,
classification: 1,
face: 1,
education: 1,
expand: "fff",
status: 0,
is_delete: 0,
updated_at: null,
created_at: "1640719204",
},
{
user_id: 11,
org_id: 1,
org_no: "01",
name: "李歌女2",
sex: 2,
cardno: "142719199711191258",
phone: "13522681234",
nation: "汉",
native: "江苏省南京市",
birthday: "19971119",
health: 1,
marital_status: 1,
classification: 1,
face: 1,
education: 1,
expand: "123",
status: 0,
is_delete: 0,
updated_at: null,
created_at: null,
},
];
业务需求:
点击数据源A中的某条数据时,获取对应B中的数据。
ES6方法:
Array.find((value,index,arr)=>{ return 条件表达式;})
value:每一次迭代查找的数组元素。
index:每一次迭代查找的数组元素索引。
arr:被查找的数组。
返回符合条件表达式的第一个value
Array.findIndex((value,index,arr)=>{ return 条件表达式;})
返回符合条件表达式的第一个value的index
实现代码:
// 假设被点击的数据是下面这条,我们用变量example接收一下
let example = {
user_id: 11,
org_id: 1,
org_no: "01",
name: "李歌女2",
sex: "男",
cardno: "142719199711191258",
phone: "13522681234",
nation: "汉",
native: "江苏省南京市",
birthday: "-",
health: "其他",
marital_status: "其他",
classification: "未定义",
face: "其他",
education: "其他",
expand: "123",
status: "其他",
is_delete: 0,
updated_at: null,
created_at: null,
org_name: "连云区一中",
};
// 在我们需要的数据源中进行查找,我们最终需要的是B中的数据,所以就在B中查找,将查到的数据用变量target接收
let target = infoB.find((value, index, arr) => {
return value.user_id === example.user_id; // 如果B中元素的user_id和example中的user_id相等,那么就返回该元素并结束find
});
在浏览器控制台输入target查看结果,符合需求。