Reputation: 169
I have this array which I've had to push the objects into the array. But now I am trying to get rid of the whole object for 'cahlan'. The for loop at the bottom is what I've tried but it doesn't seem to work.
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);
}
}
Upvotes: 0
Views: 91
Reputation: 33726
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)
Two things:
Cahlan
.name
doesn't exist.Probably you didn't realize but you're not adding the objects you've initialized, so, the array is being initialized with Strings.
Upvotes: 3
Reputation: 3372
first, use
employees.push(tyler,cahlan,ryan,colt)
without the " "
then....
employYouWant = employees.filter( (employ) => employ.name === 'Cahlan')///{name: 'Cahlan', position: 'CEO', spiritAnimal: 'butterfly'};
Upvotes: 0
Reputation: 205987
""
is a String, {}
is an Object literal
therefore employees.push("tyler", "cahlan", "ryan", "colt");
is pushing…♪ Strings.
After fixing this to - pushing your actual object literals:
employees.push(tyler, cahlan, ryan, colt);
you have finally access to your Object's .name
property.
Than you can .filter() your array:
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 )
See this more functional approach to filtering your array. The same way you can make helper functions to manage your employees Array:
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 );
or you can rewrite the above in a more generic Prototypal or Class-y way and Array agnostic.
Upvotes: -1
Reputation: 221
I just thought I could teach you how to fish first by 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);
which will be equal to the full object above,
Now there are several ways of OMITTING object from an ARRAY, some way is delete, skip in the loop as you rightly suggested above.
you can try lodash _.omit, _.concat
etc for you troubles
//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"; });
Upvotes: 0
Reputation: 302
Your solution is heading the right direction but you made two mistakes.
First of all
employees.push("tyler", "cahlan", "ryan", "colt");
does not push your objects in the array as you expect. It pushes strings into employees
which only contain the variable names of your objects. JavaScript doesn't know you're trying to refer to those objects. It thinks your just creating some text. To make it right, remove the double quotes:
employees.push(tyler, cahlan, ryan, colt);
You can always check the content of an array by printing it to the console:
console.log(employees);
Secondly, you need to pass a second parameter to splice
which tells it how many items to remove from the array. Since you only want to remove one item, do this:
employees.splice(i, 1);
Full solution:
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
Upvotes: 1
Reputation: 1579
Plain 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.
Using Jquery:
employees = $.grep(employees, function(a)) {
return a.name !== "Cahlan";
});
And you should do more searching first. See remove-object-from-array-using-javascript
Upvotes: 0