评估条件并设置标志和值的最佳表达式
Optimal expression to evaluate condition and set both flag and value
下面是两个函数,它们遍历对象集合以评估任何项目对象的 id 是否等于函数的 id 参数。如果为真,则它设置一个活动标志并将当前变量设置为等于 id。
备注:
- 函数 longerVersion(id) 如果详细/更长的方法
- 函数 shorterVersion(id) 是我目前的最佳方法
问题
- 在 ES6 和/或 lodash 中是否有更好的方法来实现相同的结果?
const items = {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:false}}
let current = 0;
function longerVersion(id) {
for (const k in this.items) {
if (this.items[k].id === id) {
this.items[k].active = true
current = id
} else {
this.items[k].active = false
}
}
}
function shorterVersion(id) {
for (const k in this.items) {
items[k].active = items[k].id === id && ((current = id) && true)
}
}
longerVersion(2);
console.log(current); // expected outcome : (current === 2)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:true}, 3:{id:3,active:false}}
shorterVersion(3);
console.log(current); // expected outcome : (current === 3)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:true}}
假设集合确实是一个普通对象并且您不需要对象原型链中的属性,in
关键字越来越不受欢迎,而有利于更新的 Object.keys
等阿尔。这是一种使用它的方法,箭头函数和可选链接,是额外的 Ecma-ish:
function ecmaVersion(id) {
const key = Object.keys(items).find((key) =>
items[key].active = (items[key].id === id))
return current = items[key]?.id
}
lodash 等效项将涉及 _.findKey
。
在函数范围内更新 current
是您要避免的副作用。而是让它成为函数的返回值。
const items = {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:false}};
const functionalVersion = (items, id) => Object.values(items).reduce((acc, x) => {
x.active = x.id === id;
return x.active ? id : acc;
}, -1);
let current = functionalVersion(items, 2);
console.log(current); // expected outcome : (current === 2)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:true}, 3:{id:3,active:false}}
current = functionalVersion(items, 3);
console.log(current); // expected outcome : (current === 3)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:true}}
当 items
id 的 none 与 id 匹配时,函数 returns a -1
。
我不喜欢表达式中的赋值,但如果那是你的事,你可以在一行中完成:
const functionalVersion = (items, id) => Object.values(items).reduce((acc, x) => x.active = x.id === id ? id : acc, -1);
下面是两个函数,它们遍历对象集合以评估任何项目对象的 id 是否等于函数的 id 参数。如果为真,则它设置一个活动标志并将当前变量设置为等于 id。
备注:
- 函数 longerVersion(id) 如果详细/更长的方法
- 函数 shorterVersion(id) 是我目前的最佳方法
问题
- 在 ES6 和/或 lodash 中是否有更好的方法来实现相同的结果?
const items = {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:false}}
let current = 0;
function longerVersion(id) {
for (const k in this.items) {
if (this.items[k].id === id) {
this.items[k].active = true
current = id
} else {
this.items[k].active = false
}
}
}
function shorterVersion(id) {
for (const k in this.items) {
items[k].active = items[k].id === id && ((current = id) && true)
}
}
longerVersion(2);
console.log(current); // expected outcome : (current === 2)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:true}, 3:{id:3,active:false}}
shorterVersion(3);
console.log(current); // expected outcome : (current === 3)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:true}}
假设集合确实是一个普通对象并且您不需要对象原型链中的属性,in
关键字越来越不受欢迎,而有利于更新的 Object.keys
等阿尔。这是一种使用它的方法,箭头函数和可选链接,是额外的 Ecma-ish:
function ecmaVersion(id) {
const key = Object.keys(items).find((key) =>
items[key].active = (items[key].id === id))
return current = items[key]?.id
}
lodash 等效项将涉及 _.findKey
。
在函数范围内更新 current
是您要避免的副作用。而是让它成为函数的返回值。
const items = {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:false}};
const functionalVersion = (items, id) => Object.values(items).reduce((acc, x) => {
x.active = x.id === id;
return x.active ? id : acc;
}, -1);
let current = functionalVersion(items, 2);
console.log(current); // expected outcome : (current === 2)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:true}, 3:{id:3,active:false}}
current = functionalVersion(items, 3);
console.log(current); // expected outcome : (current === 3)
console.log(items); // expected outcome : items: {1:{id:1,active:false},2:{id:2,active:false}, 3:{id:3,active:true}}
当 items
id 的 none 与 id 匹配时,函数 returns a -1
。
我不喜欢表达式中的赋值,但如果那是你的事,你可以在一行中完成:
const functionalVersion = (items, id) => Object.values(items).reduce((acc, x) => x.active = x.id === id ? id : acc, -1);