我希望能够向字典添加属性,但前提是我传入的条件为真。例如:
def addSum(num):
obj = {
'name': "Home",
'url': "/",
num > 0 ? 'data': num
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我找不到在 python 中执行此操作的方法,我只看到了 javascript 中的示例。
我已经看过这篇文章,并想知道是否有一种方法可以使用javascript编写一个函数,可以通过使用具有新值的另一个对象来有条件地更新对象的所有属性?
说我有这个对象:
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
Run Code Online (Sandbox Code Playgroud)
我想使用另一个对象传递的属性更新其值,如下所示:
let newObj = {test2: 'newValue2'}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我只想更新属性"test2".如果我知道我要更新的属性并且我事先知道原始属性,我将使用:
oldObj = {
...(newObj.test1) && {test1: newObj.test1} || {test1: oldObj.test1},
...(newObj.test2) && {test2: newObj.test2} || {test2: oldObj.test2},
...(newObj.test3) && {test3: newObj.test3} || {test3: oldObj.test3},
};
Run Code Online (Sandbox Code Playgroud)
意思是我只会在属性进入新对象时更新值,但当然我必须添加与对象中的属性一样多的条件.
这种方法很好,但它没有推广,所以如果对象有10个属性,我将不得不写出10个条件.
有没有办法编写一个可以有条件地更新属性的函数,这样我就不必编写10个(或更多)条件?
我正在尝试解决具有形式和功能的此问题。以下是该问题的两个示例,一个功能失调,但具有我想要达到的形式水平,另一个功能正常,但乏味。
const foo = "Spam";
const bar = () => "Ham";
const qux = (bool) => bool ? "Eggs" : "Beans";
// This is roughly the level of succintless I'd like to achieve
function genObjectPretty(request) {
return {
foo: request.foo && foo,
bar: request.bar && bar(),
qux: request.qux && qux(true),
}
}
console.log("BAD results, with SHORT code. (bar should not be included)")
console.log(genObjectPretty({foo: true, qux: true}));
// This is the working code which I'd like to make less verbose
function …Run Code Online (Sandbox Code Playgroud)这是我的代码:
app.post('/ujfeladat', (req, res) => {
const {nev, tipus, szid} = req.body;
const hianyos = () => {
if(tipus === 'hianyos'){
return {rang: -1}
}
return
}
db('fl').insert({
nev: nev,
tipus: tipus,
szid: szid,
hianyos() //returns an error
}).returning('*')
.then(data => res.json(data))
.catch(err => console.log(err))
})Run Code Online (Sandbox Code Playgroud)
我该怎么做才能将rang属性添加到对象中tipus === 'hianyos'?
我知道使用 ES6 JavaScript 对象,您可以使用 动态地将变量声明为对象键[],例如:
{[2 + 2]: "four!"}
给出输出{"4": "four!"}
问题是,是否可以使用类似的方法来通过变量等内联添加整个属性?意思是,假设我有以下测试对象:
var myObj = {
someProp: 2,
someOtherProp: 3 //only add this prop if a condition is met
}
Run Code Online (Sandbox Code Playgroud)
我可以在 for 的内联对象中编写什么内容someOtherProp,以便仅在满足特定条件时将其添加到对象中吗?例如(伪代码),像这样
var myObj = {
someProp: 2,
[someBool ? null : "someOtherProp: 3"] //only add this prop if a condition is met
}
Run Code Online (Sandbox Code Playgroud)
会给出输出(考虑 someBool 为 true),如上面所示,但如果someBool为 false,它会给我
var myObj = {
someProp: 2
}
Run Code Online (Sandbox Code Playgroud)
??
我知道我可以稍后使用 [] 索引器向对象添加属性(或删除属性),例如
someBool && …Run Code Online (Sandbox Code Playgroud) 我想有条件地在对象上创建属性。这个想法是确保该属性在没有值的情况下不存在(因此不仅仅是 null)。
我现在正在做什么:
// comes from users
req.query = {
foo: true
}
// my object which will contains allowed properties IF EXISTS
const where = {}
if (req.query.foo) {
where.foo = req.query.foo
}
if (req.query.bar) {
where.bar = req.query.bar
}
console.log(where)Run Code Online (Sandbox Code Playgroud)
对每个不同的属性重复这个真的很无聊......
有没有一种方法可以在一行中完成?
编辑:
我不想获取所有属性req.query