我必须删除与我的模型不匹配的不需要的对象属性。我怎样才能用lodash实现它。
我的模特是
var model = {
fname:null,
lname:null
}
发送到服务器之前我的控制器输出将是
var credentials = {
fname:"xyz",
lname:"abc",
age:23
}
如果我使用
_.extend (model, credentials)
我也正在获得年龄。我知道我可以使用
delete credentials.age
但如果我有超过10个不需要的对象怎么办?我可以用lodash实现吗。
答案 0 :(得分:100)
您可以从白名单或黑名单方式接近它:
// Black list
// Remove the values you don't want
var result = _.omit(credentials, ['age']);
// White list
// Only allow certain values
var result = _.pick(credentials, ['fname', 'lname']);
如果它是可重用的业务逻辑,您也可以将其分开:
// Partial out a black list version
var clean = _.partial(_.omit, _, ['age']);
// and later
var result = clean(credentials);
答案 1 :(得分:35)
使用_.keys()
从model
获取属性列表,并使用_.pick()
将属性从credentials
提取到新对象:
var model = {
fname:null,
lname:null
};
var credentials = {
fname:"xyz",
lname:"abc",
age:23
};
var result = _.pick(credentials, _.keys(model));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>
如果您不想使用lodash,可以使用Object.keys()
和Array.prototype.reduce()
:
var model = {
fname:null,
lname:null
};
var credentials = {
fname:"xyz",
lname:"abc",
age:23
};
var result = Object.keys(model).reduce(function(obj, key) {
obj[key] = credentials[key];
return obj;
}, {});
console.log(result);
答案 2 :(得分:4)
您可以使用_.pick:
轻松完成此操作
var model = {
fname:null,
lname:null
};
var credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
var result = _.pick(credentials, _.keys(model));
console.log('result =', result);
<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>
但你可以简单地使用纯JavaScript(特别是如果你使用ES6),就像那样:
const model = {
fname: null,
lname: null
};
const credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
const newModel = {};
Object.keys(model).forEach(key => newModel[key] = credentials[key]);
console.log('newModel =', newModel);
答案 3 :(得分:2)
在这里,我通过使用lodash库对要删除的各个“键”使用了省略:
private static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream("service-account.json"))
.createScoped(Arrays.asList(SCOPES));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
答案 4 :(得分:1)
如果对象较少,则可以使用_.omit()
从json数组中发出密钥
_.forEach(data,(d)=>{
_.omit(d,['keyToEmit1','keyToEmit2'])
});
如果有更多对象,则可以使用其相反的_.pick()
_.forEach(data,(d)=>{
_.pick(d,['keyToPick1','keyToPick2'])
});
答案 5 :(得分:0)
要深入选择(或删除)满足给定条件的对象属性,可以使用类似以下内容的
:function pickByDeep(object, condition, arraysToo=false) {
return _.transform(object, (acc, val, key) => {
if (_.isPlainObject(val) || arraysToo && _.isArray(val)) {
acc[key] = pickByDeep(val, condition, arraysToo);
} else if (condition(val, key, object)) {
acc[key] = val;
}
});
}
答案 6 :(得分:0)
这是我用lodash深度删除空属性的解决方案:
const compactDeep = obj => {
const emptyFields = [];
function calculateEmpty(prefix, source) {
_.each(source, (val, key) => {
if (_.isObject(val) && !_.isEmpty(val)) {
calculateEmpty(`${prefix}${key}.`, val);
} else if ((!_.isBoolean(val) && !_.isNumber(val) && !val) || (_.isObject(val) && _.isEmpty(val))) {
emptyFields.push(`${prefix}${key}`);
}
});
}
calculateEmpty('', obj);
return _.omit(obj, emptyFields);
};
答案 7 :(得分:0)
Lodash unset
适合删除一些不需要的键。
const myObj = {
keyOne: "hello",
keyTwo: "world"
}
unset(myObj, "keyTwo");
console.log(myObj); /// myObj = { keyOne: "hello" }