给一个数组对象,再给一个数组字符串,筛选出包含数组字符串的数组对象部分
const arr1 = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 3, name: 'C' }]
const arr2 = [1, 2]
const arr3 = arr1.filter(item => arr2.includes(item.id))
// arr3为[{ id: 1, name: 'A' }, { id: 2, name: 'B' }]
// 意思是,arr2中includes包含arr1id的为true,arr1再filter筛选出来
删除指定元素
方法一:
/* 删除数组中指定函数 */
delete(arr, item1) {
// eslint-disable-next-line no-shadow
const index = arr.findIndex(item => item.value === item1)
arr.splice(index, 1)
},
应用
const data = ['1', '2']
this.weeks = [
{ value: '1', text: '一' },
{ value: '2', text: '二' },
{ value: '3', text: '三' },
{ value: '4', text: '四' },
{ value: '5', text: '五' },
{ value: '6', text: '六' },
{ value: '7', text: '日' },
]
data.forEach((item) => {
this.delete(this.weeks, item)
})
方法二:
// 删除数组中指定元素
removeByValue(arr, val) {
arr.forEach((item, index) => {
if (item === val) {
arr.splice(index, 1)
}
})
},