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);
如果数组中有重复的两个元素,我们只想删除一个元素,我们必须使用另一种方法。文章来源地址https://www.toymoban.com/news/detail-532950.html
2: 数组 使用 indexOf() 和 splice()
let arr = [ "🐤", "你", "太", "美", "🐤" ];
let arrList = arr.indexOf("🐤");
arr.splice(arrList, 1);
console.log(arr);
3. 数组(数组对象) for循坏
// 数组
let arr = [1,2,3,4,5,6]
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);
文章来源:https://www.toymoban.com/news/detail-532950.html
到了这里,关于JavaScript如何从数组(数组对象)中删除特定数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!