javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > JS修改、删除数组某个属性

JavaScript修改、删除数组中某个对象的某个属性几种方法

作者:William_jade

在JavaScript开发中,经常需要修改数组中对象的属性,下面这篇文章主要介绍了JavaScript修改、删除数组中某个对象的某个属性几种方法,需要的朋友可以参考下

一  有几种方法可以用来修改JavaScript数组中对象的属性:

方法1:使用Array.map()方法

使用map()方法根据指定的函数,通过转换原始数组的每个元素来创建一个新的数组。

let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
 
const modifiedEmployees = employees_data.map(obj => {
    if (obj.employee_id === 2) {
        return { ...obj, employee_name: "rahul" };
    }
    return obj;
});
 
console.log(modifiedEmployees);

输出

[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'rahul' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

方法2:使用for-of循环

使用for循环遍历一个包含对象的数组,根据条件查找并修改特定对象的属性。

let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
for (let object of employees_data) {
    if (object.employee_id === 2) {
        object.employee_name = "Anthony";
    }
}
console.log("Updated Data: ");
console.log(employees_data);

输出

Updated Data: 
[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'Anthony' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

方法3:使用Array.map()与扩展运算符

使用Array.map()创建一个带有扩展运算符的新数组,以修改特定对象的属性。

employees_data.map((employee) => {  
    if (employee.employee_id === 2) {  
        return {  
            ...employee,  
            employee_name: "Anthony",  
        };  
    }  
    return employee;  
});

示例: 在这个示例中,我们将使用Array.map()方法以及spread operator(…)将对象自身展开,以便通过使用先前创建的对象数组来更新现有对象的属性值。

let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
let new_updated_data =
    employees_data.map((employee) => {
        if (employee.employee_id === 2) {
            return {
                ...employee,
                employee_name: "Anthony",
            };
        }
        return employee;
    });
console.log("Updated Data: ");
console.log(new_updated_data);

输出

Updated Data: 
[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'Anthony' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

方法4:使用 forEach() 方法

使用 forEach() 方法,遍历对象数组,检查条件并修改匹配对象的属性

let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
 
const modifyProperty = (arr, targetId, newProperty) => {
    arr.forEach(obj => {
        if (obj.employee_id === targetId) {
            obj.employee_name = newProperty;
        }
    });
};
 
modifyProperty(employees_data, 2, "Ankit");
console.log(employees_data);
[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'Ankit' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

方法5:使用find()方法和解构

使用find()方法搜索具有指定属性值的对象,并使用解构修改属性。

let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
 
const modifyProperty = (arr, targetId, newProperty) => {
    const targetObj = arr.find(obj => obj.employee_id === targetId);
    if (targetObj) {
        targetObj.employee_name = newProperty;
    }
};
 
modifyProperty(employees_data, 2, "Kavita");
console.log(employees_data);
[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'Kavita' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

二  JavaScript删除数组对象中的某个对象里的某个属性

方法1:使用for循环

let employ = [
    {
        employid: 1,
        employname: "Aman",
        employage:"15"
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
        employage:"16"
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
        employage:"17"
    },
];
       for(var i =0;i<employ.length;i++){
        if(employ[i].employid===1){
           delete employ[i].employage
        }}
console.log(employ)

输出:
[
    {
        employid: 1,
        employname: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
        employage:"16"
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
        employage:"17"
    },
];

可以看到上面打印输出第一条对象里的年龄属性删除了

总结

到此这篇关于JavaScript修改、删除数组中某个对象的某个属性几种方法的文章就介绍到这了,更多相关JS修改、删除数组某个属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文