我试图摆脱属性5MinuteRate
和15MinuteRate
以下对象.
var object = { requestsPerSecond: { mean: 1710.2180279856818, count: 10511, 'currentRate': 1941.4893498239829, '1MinuteRate': 168.08263156623656, '5MinuteRate': 34.74630977619571, '15MinuteRate': 11.646507524106095 } };
Lodash的omit() - 函数似乎不适用于嵌套对象.以下代码不起作用:
console.log(_.omit(object, 'requestsPerSecond.count'));
编辑:
我尝试了这个,但它不能正常工作:
var subObject = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate'); console.log(_.merge(object, subObject));
J. Titus.. 6
你快到了.刚分配这将是你的结果subObject
来object.requestsPerSecond
.
var object = {
requestsPerSecond: {
mean: 1710.2180279856818,
count: 10511,
'currentRate': 1941.4893498239829,
'1MinuteRate': 168.08263156623656,
'5MinuteRate': 34.74630977619571,
'15MinuteRate': 11.646507524106095
}
};
object.requestsPerSecond = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate');
console.log(object);
你快到了.刚分配这将是你的结果subObject
来object.requestsPerSecond
.
var object = {
requestsPerSecond: {
mean: 1710.2180279856818,
count: 10511,
'currentRate': 1941.4893498239829,
'1MinuteRate': 168.08263156623656,
'5MinuteRate': 34.74630977619571,
'15MinuteRate': 11.646507524106095
}
};
object.requestsPerSecond = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate');
console.log(object);
使用unset:https : //lodash.com/docs#unset,它会变得更干净:
var obj = { requestsPerSecond: { mean: 1710.2180279856818, count: 10511, 'currentRate': 1941.4893498239829, '1MinuteRate': 168.08263156623656, '5MinuteRate': 34.74630977619571, '15MinuteRate': 11.646507524106095 } }; _.forEach(['requestsPerSecond.5MinuteRate', 'requestsPerSecond.15MinuteRate'], function(omitProperty) { obj = _.unset(obj, omitProperty); } ); // Or avoiding the "extra" loop. obj = _.unset(obj, 'requestsPerSecond.5MinuteRate'); obj = _.unset(obj, 'requestsPerSecond.15MinuteRate');