简体   繁体   English

如何在 javascript 的数组中删除 object

[英]How to delete an object in array in javascript

[
  Employee {
    empId: 1000,
    empName: 'John',
    empSalary: 9123,
  },
  Employee {
    empId: 1001,
    empName: 'Doe',
    empSalary: 10000,
  },
  Employee {
    empId: 1002,
    empName: 'Jason',
    empSalary: 9812,
  },
  Employee {
    empId: 1003,
    empName: 'Jamie',
    empSalary: 6661,
  }
]

This is my array with set of objects.这是我的一组对象数组。 I want to take a user input and delete from the array based on ID.我想根据 ID 获取用户输入并从数组中删除。 I've tried using splice but I keep getting errors.我尝试过使用 splice,但我不断收到错误消息。

var target = prompt("Enter id to delete:");
for(var i = 0; i < data.length; i++) {
    if(data[i].empId === target) {
        data.splice(i, 1);
        break;
    }
}

If someone can please help me out please do!如果有人可以请帮助我,请做!

The id values in the array are integers.数组中的 id 值是整数。 The information you add at the prompt is a string.您在提示符处添加的信息是一个字符串。 All you have to do is cast the string to a number .您所要做的就是将字符串转换为数字

 const data = [{"empId":1000,"empName":"John","empSalary":9123},{"empId":1001,"empName":"Doe","empSalary":10000},{"empId":1002,"empName":"Jason","empSalary":9812},{"empId":1003,"empName":"Jamie","empSalary":6661}]; var target = +prompt("Enter id to delete:"); // ----------^ for (let i = 0; i < data.length; i++) { if (data[i].empId === target) { data.splice(i, 1); break; } } console.log(data);

Alternatively, and the preferred option these days, you can use filter to create a new array of updated data:或者,这些天的首选选项是,您可以使用filter创建一个新的更新数据数组:

 const data = [{"empId":1000,"empName":"John","empSalary":9123},{"empId":1001,"empName":"Doe","empSalary":10000},{"empId":1002,"empName":"Jason","empSalary":9812},{"empId":1003,"empName":"Jamie","empSalary":6661}]; var target = +prompt("Enter id to delete:"); const newData = data.filter(({ empId }) => empId;== target). console;log(newData);

probably the variable target is type String , but the empId is type Number .变量target可能是String类型,但empIdNumber类型。 So that '1' is not equal to 1. I think you should replace this line:所以'1'不等于1。我认为你应该替换这一行:

if(data[i].empId === target) {

with the following line:使用以下行:

if(data[i].empId === +target) {

Couldn't find any errors, but you have to convert the prompt result to number since the returned data is of type string.找不到任何错误,但是由于返回的数据是字符串类型,因此您必须将提示结果转换为数字。

 let data = [{ empId: 1000, empName: 'John', empSalary: 9123, }, { empId: 1001, empName: 'Doe', empSalary: 10000, }, { empId: 1002, empName: 'Jason', empSalary: 9812, }, { empId: 1003, empName: 'Jamie', empSalary: 6661, } ] var target = prompt("Enter id to delete:"); for (var i = 0; i < data.length; i++) { if (data[i].empId === Number(target)) { data.splice(i, 1); break; } } console.log(data)

Never a good idea to mutate the array you are traversing.改变你正在遍历的数组从来都不是一个好主意。 Use newer standards like filter to have a different array and leave the original unchanged.使用过滤器等较新的标准来拥有不同的数组并保持原始数组不变。

Also, you might have issues with the type of data.此外,您可能对数据类型有疑问。 '===' does strict type checking. '===' 进行严格的类型检查。 You can either use '==' or parse the target into a number.您可以使用 '==' 或将目标解析为数字。

let newData = data.filter((employee) => {
return (employee.empId != target)
});

newData now has your array with employee with target id removed. newData 现在有您的数组,其中包含删除了目标 ID 的员工。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM