JavaScript如何从数组(数组对象)中删除特定数据

1:数组 (数组对象) 使用 filter()

// 数组
let arr = [ "", "你", "太", "美" ];
let arrList = arr.filter((x) => x !== "太");
console.log(arrList);

// 数组对象
let arr = [{name:"张三",age:18},{name:"李四",age:19},{name:"王五",age:20}]
let list = arr.filter((val) => val.name !== '张三')
console.log(list);

如果数组中有重复的两个元素,我们只想删除一个元素,我们必须使用另一种方法。

2: 数组 使用 indexOf() 和 splice()

let arr = [ "", "你", "太", "美", "" ];
let arrList = arr.indexOf("");
arr.splice(arrList, 1);
console.log(arr);

3. 数组(数组对象) for循坏


// 数组
let arr = [123456]
let list = []
console.log(arr.indexOf(4));
for(let i=0;i<arr.length;i++){
   if(i !== arr.indexOf(4)){
      list.push(arr[i])
   }
}
console.log(list);

// 对象数组
let arr = [{name:"张三",age:18},{name:"李四",age:19},{name:"王五",age:20}]
let list = []
for(let i=0;i<arr.length;i++){
    if(arr[i].name !== '张三'){
       list.push(arr[i])
      }
   }
console.log(list);

你可能感兴趣的:(javascript,前端,vue.js)