Skip to content

删除对象中的某个参数

基本使用

vue
<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'
import { removearray } from 'vue-hkp-ui'

const array_1=[{name:'我是name1'},{name:'我是name2'}]
const array_2=['我是name1','我是name2']
console.log(removearray(array_1,{name:'我是name1'},'name'))  //输出 [{name:'我是name2'}]
console.log(removearray(array_2,'我是name1'))  //输出 我是name2

</script>

内部方法

vue
<script setup lang="ts">
function removearray(arr, ele, parameter=''){
  // 判断数组中是对象数组还是字符串数组
  let isStringArray = arr.every(element => typeof element === 'string'); 
  if(isStringArray){
    let index = arr.indexOf(ele);
    if (index > -1) {
        arr.splice(index, 1);
    }
    return arr;
  }else{
    let newList:any= []
    arr.forEach((item:any) => {
        if(item[parameter].indexOf(ele[parameter]) === -1){
            newList.push(item)
        }
    })
    return newList;
  }
}
</script>

removearray Params

参数说明类型默认值必传
arr当前数组array-true
ele删除的对象object/string-true
parameter对比的属性(类似案例中的name)stringfalse

Released under the MIT License.