如何从 javascript 中可能有也可能没有数据的已知键正确构建对象
How to correctly build an object from known keys that may or may not have data in javascript
我想创建一个对象,虽然我知道这些键,但我实际上并不希望它们出现在对象中,除非它们的值大于零。
这是我所拥有的,它有效,我只是想知道这是否是最优雅的方法。
var tabs = {};
if(shot.attachments_count !== 0) {
tabs.attachments = shot.attachments_count;
}
if(shot.buckets_count !== 0) {
tabs.buckets = shot.buckets_count;
}
if(shot.comments_count !== 0) {
tabs.comments = shot.comments_count;
}
if(shot.likes_count !== 0) {
tabs.likes = shot.likes_count;
}
if(shot.rebounds_count !== 0) {
tabs.rebounds = shot.rebounds_count;
}
更新
好的,这就是我所经历的:
var tabs = {},
keys = [
'attachments',
'buckets',
'comments',
'likes',
'rebounds'
];
keys.forEach(function(i) {
if(shot[i + '_count'] > 0) {
tabs[i] = shot[i + '_count'];
}
});
我必须使用数组,因为我知道键,而且因为我正在使用 dribbble api 并且镜头中还有更多属性,所以我不能像我想要的那样遍历所有内容以上在我的对象中。
我也在使用车把,所以我正在构建一个对象来循环遍历键是我的车把项目和镜头。*_count 是 dribbble api 值。
我希望这能更好地解释我在做什么,感谢您的帮助
你可以遍历这些键,将它们添加到tabs
,只有它们满足条件,像这样
var tabs = {};
Object.keys(shot).forEach(function(currentProperty) {
// Check if the value corresponding to current property is greater than 0
if (shot[currentProperty] > 0) {
tabs[currentProperty.replace('_count', '')] = shot[currentProperty];
}
});
正如 T.J.Crowder 所建议的,如果键列表是事先已知的,您可以将它们设为数组并仅迭代它们,就像这样
var keys = ['attachments_count', 'buckets_count' ... ];
keys.forEach(...);
这样会更好,因为您不会不小心从 shot
复制其他不需要的属性,如果有的话。
因为它是一个计数器,我们假设它们总是有一个数值,并且它将是 >=0
。此外,您总是想从密钥中删除 __count
。那么你可以这样做:
var tabs = {};
for(key in shot){
if(!shot[key]){
continue;
}
tabs[key.substr(0, key.length - 6)] = shot[key];
}
var shotKeys = Object.keys(shot);
var tabKeys = [];
var tab = {};
for (var i = 0; i < shotKeys.length; i++) {
tabKeys.push(shotKeys[i].split('_')[0]);
if (shot[shotKeys[i]] > 0) {
tab[tabKeys[i]] = shot[shotKeys[i]];
}
}
我想创建一个对象,虽然我知道这些键,但我实际上并不希望它们出现在对象中,除非它们的值大于零。
这是我所拥有的,它有效,我只是想知道这是否是最优雅的方法。
var tabs = {};
if(shot.attachments_count !== 0) {
tabs.attachments = shot.attachments_count;
}
if(shot.buckets_count !== 0) {
tabs.buckets = shot.buckets_count;
}
if(shot.comments_count !== 0) {
tabs.comments = shot.comments_count;
}
if(shot.likes_count !== 0) {
tabs.likes = shot.likes_count;
}
if(shot.rebounds_count !== 0) {
tabs.rebounds = shot.rebounds_count;
}
更新
好的,这就是我所经历的:
var tabs = {},
keys = [
'attachments',
'buckets',
'comments',
'likes',
'rebounds'
];
keys.forEach(function(i) {
if(shot[i + '_count'] > 0) {
tabs[i] = shot[i + '_count'];
}
});
我必须使用数组,因为我知道键,而且因为我正在使用 dribbble api 并且镜头中还有更多属性,所以我不能像我想要的那样遍历所有内容以上在我的对象中。
我也在使用车把,所以我正在构建一个对象来循环遍历键是我的车把项目和镜头。*_count 是 dribbble api 值。
我希望这能更好地解释我在做什么,感谢您的帮助
你可以遍历这些键,将它们添加到tabs
,只有它们满足条件,像这样
var tabs = {};
Object.keys(shot).forEach(function(currentProperty) {
// Check if the value corresponding to current property is greater than 0
if (shot[currentProperty] > 0) {
tabs[currentProperty.replace('_count', '')] = shot[currentProperty];
}
});
正如 T.J.Crowder 所建议的,如果键列表是事先已知的,您可以将它们设为数组并仅迭代它们,就像这样
var keys = ['attachments_count', 'buckets_count' ... ];
keys.forEach(...);
这样会更好,因为您不会不小心从 shot
复制其他不需要的属性,如果有的话。
因为它是一个计数器,我们假设它们总是有一个数值,并且它将是 >=0
。此外,您总是想从密钥中删除 __count
。那么你可以这样做:
var tabs = {};
for(key in shot){
if(!shot[key]){
continue;
}
tabs[key.substr(0, key.length - 6)] = shot[key];
}
var shotKeys = Object.keys(shot);
var tabKeys = [];
var tab = {};
for (var i = 0; i < shotKeys.length; i++) {
tabKeys.push(shotKeys[i].split('_')[0]);
if (shot[shotKeys[i]] > 0) {
tab[tabKeys[i]] = shot[shotKeys[i]];
}
}