So I am still learning arrays and objects, and I'm a little bit stuck. I made an example of array of objects:
var something = {candy:[{name:"snickers", price:2},
{name:"bounty", price:3},
{name:"mars", price:4}]};
Object.keys(something.candy).filter(function(obj){
return console.log(something.candy[obj].name);
})
1. Question - Why doesn't it work when I write.:
var candy1 = Object.keys(something.candy);
candy1.filter(function(obj){
return console.log(obj.name);
});
Isn't it practically the same code meaning as above?
2.Question How e slice works and splice doesn't???
Object.keys(something.candy).filter(function(obj){
return console.log(something.candy[obj].name.slice(0,3));
})
Object.keys(something.candy).filter(function(obj){
return a(something.candy[obj].name.splice(1,3));
})
So I am still learning arrays and objects, and I'm a little bit stuck. I made an example of array of objects:
var something = {candy:[{name:"snickers", price:2},
{name:"bounty", price:3},
{name:"mars", price:4}]};
Object.keys(something.candy).filter(function(obj){
return console.log(something.candy[obj].name);
})
1. Question - Why doesn't it work when I write.:
var candy1 = Object.keys(something.candy);
candy1.filter(function(obj){
return console.log(obj.name);
});
Isn't it practically the same code meaning as above?
2.Question How e slice works and splice doesn't???
Object.keys(something.candy).filter(function(obj){
return console.log(something.candy[obj].name.slice(0,3));
})
Object.keys(something.candy).filter(function(obj){
return a(something.candy[obj].name.splice(1,3));
})
Share
Improve this question
asked Nov 11, 2017 at 0:42
Eleven11Eleven11
951 gold badge3 silver badges6 bronze badges
3
-
4
something.candy
is already an array. Don't useObject.keys
on it, just use filter directly:something.candy.filter( function(obj) { obj.name; } )
. – ibrahim mahrir Commented Nov 11, 2017 at 0:45 -
slice
work because there is a functionString.prototype.slice
andsplice
doesn't because there is no functionString.prototype.splice
. Becausename
is a string. Only arrays havesplice
. – ibrahim mahrir Commented Nov 11, 2017 at 0:56 - I get it now thank you very much!!!! @ibrahimmahrir – Eleven11 Commented Nov 11, 2017 at 1:01
1 Answer
Reset to default 3It's helpful to pull each piece apart and look at it when learning this. For example you have an object:
var something = {candy:[{name:"snickers", price:2},
{name:"bounty", price:3},
{name:"mars", price:4}]};
// what is something.candy:
console.log("candy:", something.candy)
// an array -- so what do you get with Object.keys?
var candy1 = Object.keys(something.candy)
console.log("keys:", candy1)
// just the indexes!
// So when you try to filter
candy1.filter(function(obj){
// there is no obj.name becuase each object is just a number
return console.log(obj);
});
I think you want to just do this:
var something = {candy:[{name:"snickers", price:2},
{name:"bounty", price:3},
{name:"mars", price:4}]};
var select = something.candy.filter(obj => {
console.log(obj.name) // look at them all
return obj.name === "mars" // just pick this one
})
console.log("filtered: ", select)