[英]How to get all not owned items in a player items array in JSON
I want to get all not owned items from a player items array what i tried我想从我尝试过的玩家物品阵列中获取所有未拥有的物品
var players = {
"player1": {
"items": [{
"item1": {
"owned": false,
"price": 3000,
},
"item2": {
"owned": true,
"price": 3000,
}
}]
}
}
Object.keys(players).forEach(player => {
for(var i =0; i < players[player].items.length; i++) {
var not_owned = players[player].items.filter(item => {return item.owned === false});
console.log(not_owned.length);
}
});
but it is not giving me anything i tried to see the length and it gives me 0,但它没有给我任何我试图看到长度的东西,它给了我 0,
i tried to console.log it without length and it gives me undefined any ideas?我试图对它进行无长度的 console.log,它给了我未定义的任何想法?
You need to get the inner items object values and then filter it您需要获取内部项目对象值,然后对其进行过滤
var players = { "player1": { "items": [{ "item1": { "owned": false, "price": 3000, }, "item2": { "owned": true, "price": 3000, } }] } } Object.keys(players).forEach(player => { for(var i =0; i < players[player].items.length; i++) { var not_owned = Object.entries(players[player].items[i]).filter(([key, item]) => {return item.owned === false}); console.log(not_owned.length); } });
You don't need the for
loop in there.你不需要在那里
for
循环。 Now for every iteration of the forEach
the not owned will contain the items you want for this player.现在,对于
forEach
的每次迭代, forEach
将包含您想要的该播放器的项目。
var players = {
"player1": {
"items": [{
"item1": {
"owned": false,
"price": 3000,
},
"item2": {
"owned": true,
"price": 3000,
}
}]
}
}
Object.keys(players).forEach(player => {
var not_owned = players[player].items.filter(item => {return item.owned === false});
console.log(not_owned.length);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.