最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object.keys, slice and splice - Stack Overflow

programmeradmin1浏览0评论

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 use Object.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 function String.prototype.slice and splice doesn't because there is no function String.prototype.splice. Because name is a string. Only arrays have splice. – 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
Add a ment  | 

1 Answer 1

Reset to default 3

It'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) 

发布评论

评论列表(0)

  1. 暂无评论