我有这个数组,我必须将对象推入数组。但现在我试图摆脱'cahlan'的整个对象。底部的for循环是我尝试过但它似乎不起作用。
var employees = [];
var tyler = {
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
};
var cahlan = {
name: 'Cahlan',
position: 'CEO',
spiritAnimal: 'butterfly'
};
var ryan = {
name: 'Ryan',
position: 'Marketing',
spiritAnimal: 'fox'
};
var colt = {
name: 'Colt',
position: 'Everything really',
spiritAnimal: 'Young Male Horse'
};
employees.push("tyler", "cahlan", "ryan", "colt");
for (var i = 0; i < employees.length; i++) {
if (employees[i].name === "Cahlan") {
employees.splice(i);
}
}
答案 0 :(得分:3)
var employees = [];
employees.push("tyler", "cahlan", "ryan", "colt");
for (var i = 0; i < employees.length; i++) {
if (employees[i] === "cahlan") {
employees.splice(i, 1);
break;
}
}
console.log(employees)
&#13;
两件事:
Cahlan
进行比较。name
不存在。可能你没有意识到,但是你没有添加你已经初始化的对象,因此,数组正在用字符串进行初始化。
答案 1 :(得分:1)
你的解决方案正朝着正确的方向前进,但你犯了两个错误。
首先
employees.push("tyler", "cahlan", "ryan", "colt");
不会按预期将数组推送到数组中。它将字符串推送到employees
,它只包含对象的变量名称。 JavaScript并不知道您正在尝试引用这些对象。它认为你只是创建一些文本。要做到正确,请删除双引号:
employees.push(tyler, cahlan, ryan, colt);
您可以随时通过将数组打印到控制台来检查数组的内容:
console.log(employees);
其次,您需要将第二个参数传递给splice
,它告诉它要从数组中删除多少项。由于您只想删除一个项目,请执行以下操作:
employees.splice(i, 1);
完整解决方案:
var employees = [];
var tyler = {
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
};
var cahlan = {
name: 'Cahlan',
position: 'CEO',
spiritAnimal: 'butterfly'
};
var ryan = {
name: 'Ryan',
position: 'Marketing',
spiritAnimal: 'fox'
};
var colt = {
name: 'Colt',
position: 'Everything really',
spiritAnimal: 'Young Male Horse'
};
employees.push(tyler, cahlan, ryan, colt); // 1.) remove double quotes
console.log(employees); // print array to console to check if array is setup right
for (var i = 0; i < employees.length; i++) {
if (employees[i].name === "Cahlan") {
employees.splice(i, 1); // 2.) add second paramter
}
}
console.log(employees); // print array again to see if removing worked
&#13;
答案 2 :(得分:0)
普通javascript:
var ret = [];
for (var i = 0; i < employees.length; i++) {
if (employees[i].name !== "Cahlan") ret.push(employees[i]);
}
//ret has the employees with Cahlan removed.
使用Jquery:
employees = $.grep(employees, function(a)) {
return a.name !== "Cahlan";
});
你应该先做更多的搜索。见remove-object-from-array-using-javascript
答案 3 :(得分:0)
我只是想我可以通过properly working with objects
教你如何钓鱼var employees = [
{
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
},
{
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
},
{
name: 'Cahlan',
position: 'CEO',
spiritAnimal: 'butterfly'
},
{
name: 'Ryan',
position: 'Marketing',
spiritAnimal: 'fox'
},
{
name: 'Colt',
position: 'Everything really',
spiritAnimal: 'Young Male Horse'
}];
var obj = Object.assign({}, tyler, cahlan, ryan, colt);
将等于上面的完整对象,
现在有几种方法可以从ARRAY中添加OMITTING对象,有些方法是删除,在上面正确建议的循环中跳过。
你可以试试lodash _.omit, _.concat
等麻烦
//1
employees.shift(); // first element removed
//2
employees= employees.slice(1); // first element removed
//3
employees.splice(0,1); // first element removed
//4
employees.pop(); // last element removed
// 5 return everything but Cahlan
employees.filter(function(arr) { return arr.name !== "Cahlan"; });
// 5 return only Cahlan
employees.filter(function(arr) { return arr.name === "Cahlan"; });
答案 4 :(得分:0)
首先,使用
employees.push(tyler,cahlan,ryan,colt)
没有" "
然后...
employYouWant = employees.filter( (employ) => employ.name === 'Cahlan')///{name: 'Cahlan', position: 'CEO', spiritAnimal: 'butterfly'};
答案 5 :(得分:-1)
""
是一个String,{}
是一个Object literal
因此employees.push("tyler", "cahlan", "ryan", "colt");
正在推动......♪弦乐。
将此修复后 - 推送您的实际对象文字:
employees.push(tyler, cahlan, ryan, colt);
您最终可以访问对象的.name
属性
你可以 .filter()你的阵列:
var employees = [];
var tyler = {
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
};
var cahlan = {
name: 'Cahlan',
position: 'CEO',
spiritAnimal: 'butterfly'
};
var ryan = {
name: 'Ryan',
position: 'Marketing',
spiritAnimal: 'fox'
};
var colt = {
name: 'Colt',
position: 'Everything really',
spiritAnimal: 'Young Male Horse'
};
employees.push(tyler, cahlan, ryan, colt); // Push your actual Object literals
employees = employees.filter( obj => obj.name !== "Cahlan" );
console.log( employees )
请参阅此更具功能性的方法来过滤您的阵列。您可以使用辅助函数来管理员工数组:
let employees = [];
const addEmployee = (name, position, spiritAnimal) =>
employees.push({name, position, spiritAnimal});
const removeEmployee = (name) =>
employees = employees.filter( obj => obj.name !== name );
// Let's add some to Array
addEmployee('Tyler', 'Lead Instructor/Engineer', 'Honey Badger');
addEmployee('Cahlan', 'CEO', 'butterfly');
addEmployee('Ryan', 'Marketing', 'fox');
addEmployee('Colt', 'Everything really', 'Young Male Horse');
// Let's remove some from Array
removeEmployee('Cahlan');
removeEmployee('Colt');
console.log( employees );
或者你可以用更通用的 Prototypal 或 Class -y方式和数组不可知的方式重写上述内容。