写法
判断字符串是否为空
if (str !== null && str !== undefined && str !== '') {
}
在方法内写一个字典
const values = {
1: '1教室',
2: '2教室',
};
两个数组取交集
let U8CList = [
{ code: 'U8C1', id: 1 },
{ code: 'U8C2', id: 2 },
];
let YSList = [
{ code: 'YS1', id: 10 },
{ code: 'YS2', id: 11 },
];
let result = YSList.filter(ys => U8CList.some(u8c => u8c.code === ys.code));
console.log(result);
将一个对象型数组按照对象的属性去重
const array = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'John' },
{ id: 4, name: 'Doe' }
];
const uniqueByProperty = (array, property) => {
return array.filter((item, index) => {
return array.findIndex((other) => other[property] === item[property]) === index;
});
};
函数
对象型数组通过属性获取其他属性值,(当成map用)
function getNameById(array, code) {
const foundObject = array.find((obj) => obj.code === code);
return foundObject ? foundObject.id : null;
}