如何使用 UnderscoreJS、Omit 和少数条件组合所有内容?
How can I combine all using UnderscoreJS, Omit and few conditions?
const objA = {
name: 'jack',
age: '24',
address : '#210-1101',
zip : '10114',
Zip1 : '22222',
accountNo : '12345667890',
bankName : 'KB',
amount : '1000',
currency : 'USD',
country : 'USA'
};
const objB = {
name: 'eddie',
lastName : 'bauer',
addressDetail : '',
address: '#210-1101',
zip: '10114',
currency : 'EUR',
country : 'FRA'
};
const isSame = (v,k) => objA[k] === v;
const tmpRet = _(objB).omit(isSame);
const isName = (tmpRet, k) => k === 'name' ? Object.assign(tmpRet, {date: '19780708'}, {}) : '';
const isCurrency = (tmpRet, k) => k === 'currency' ? Object.assign(tmpRet, {currency2: 'USD'}, {}) : '';
const checkCondition = (tmpRet, k) => { isName(tmpRet, k); isCurrency(tmpRet, k); };
const goFunc = () => _(tmpRet).each( (v,k) => checkCondition(tmpRet, k));
goFunc();
console.log(tmpRet );
我有一个类似于上面的代码,但我无法将它们全部放在一起。
我想让它更实用,更单行。
有什么建议吗??
谢谢
更新
您可能正在寻找这样的东西:
import _ from "underscore";
const objA = {
name: "jack",
age: "24",
address: "#210-1101",
zip: "10114",
Zip1: "22222",
accountNo: "12345667890",
bankName: "KB",
amount: "1000",
currency: "USD",
country: "USA"
};
const objB = {
name: "eddie",
lastName: "bauer",
addressDetail: "",
address: "#210-1101",
zip: "10114",
currency: "EUR",
country: "FRA"
};
const compose = (...fns) => (tmp, k) =>
fns.reduceRight((acc, fn) => fn(acc, k), tmp);
const isSame = (v, k) => objA[k] === v;
const omit = (obj) => _(obj).omit(isSame);
const hasProperty = (obj, prop) =>
Object.prototype.hasOwnProperty.call(obj, prop);
const isName = (tmp) =>
hasProperty(tmp, "name")
? {
...tmp,
date: "19780708"
}
: tmp;
const isCurrency = (tmp, k) =>
hasProperty(tmp, "currency")
? {
...tmp,
currency2: "USD"
}
: tmp;
const goFunc = () => compose(isName, isCurrency, omit)(objB);
const result = goFunc();
我相信你可以跳过每个键的迭代
const objA = {
name: 'jack',
age: '24',
address : '#210-1101',
zip : '10114',
Zip1 : '22222',
accountNo : '12345667890',
bankName : 'KB',
amount : '1000',
currency : 'USD',
country : 'USA'
};
const objB = {
name: 'eddie',
lastName : 'bauer',
addressDetail : '',
address: '#210-1101',
zip: '10114',
currency : 'EUR',
country : 'FRA'
};
const isSame = (v,k) => objA[k] === v;
const tmpRet = _(objB).omit(isSame);
const isName = (tmpRet, k) => k === 'name' ? Object.assign(tmpRet, {date: '19780708'}, {}) : '';
const isCurrency = (tmpRet, k) => k === 'currency' ? Object.assign(tmpRet, {currency2: 'USD'}, {}) : '';
const checkCondition = (tmpRet, k) => { isName(tmpRet, k); isCurrency(tmpRet, k); };
const goFunc = () => _(tmpRet).each( (v,k) => checkCondition(tmpRet, k));
goFunc();
console.log(tmpRet );
我有一个类似于上面的代码,但我无法将它们全部放在一起。
我想让它更实用,更单行。
有什么建议吗??
谢谢
更新
您可能正在寻找这样的东西:
import _ from "underscore";
const objA = {
name: "jack",
age: "24",
address: "#210-1101",
zip: "10114",
Zip1: "22222",
accountNo: "12345667890",
bankName: "KB",
amount: "1000",
currency: "USD",
country: "USA"
};
const objB = {
name: "eddie",
lastName: "bauer",
addressDetail: "",
address: "#210-1101",
zip: "10114",
currency: "EUR",
country: "FRA"
};
const compose = (...fns) => (tmp, k) =>
fns.reduceRight((acc, fn) => fn(acc, k), tmp);
const isSame = (v, k) => objA[k] === v;
const omit = (obj) => _(obj).omit(isSame);
const hasProperty = (obj, prop) =>
Object.prototype.hasOwnProperty.call(obj, prop);
const isName = (tmp) =>
hasProperty(tmp, "name")
? {
...tmp,
date: "19780708"
}
: tmp;
const isCurrency = (tmp, k) =>
hasProperty(tmp, "currency")
? {
...tmp,
currency2: "USD"
}
: tmp;
const goFunc = () => compose(isName, isCurrency, omit)(objB);
const result = goFunc();
我相信你可以跳过每个键的迭代