[英]Typescript/Javascript assign and return one-liner
Many times I ask myself the same question... With all that syntaxes (not always intuitive) to write quite direct code in JS, I was wondering, would someone know about a one-liner for that kind of operation? 很多次我问自己一个同样的问题...用所有语法(并非总是直观)在JS中编写相当直接的代码,我想知道,有人会知道这种操作的单行代码吗?
var setFeatured = entry => {
entry.isFeatured = true;
return entry
}
SomeCallThatReturnsAPromise.then(entries => entries.map(setFeatured))
To assign a property and return the object in one shot, that I could put in a readable way directly as arg of entries.map
要分配属性并一次性返回对象,我可以以可读的方式直接将它们作为entry.map放置
entries.map
To give a feedback about what was proposed to me, the common answer was to return a result with a OR operator, after an assignation or function call (which returns undefined
, null
, false
, never
, well anything that will trigger the part after the OR): 为了向我提供有关建议的反馈,常见的答案是在
undefined
或函数调用后返回OR运算符的结果(返回undefined
, null
, false
, never
,以及将在要么):
return entry.isFeatured = true || entry
The interest of my question was to know if I could take advantage of a more compact syntax: 我的问题的兴趣在于知道我是否可以利用更紧凑的语法:
SomeCallThatReturnsAPromise()
.then((entries:EntryType[]) => entries
.map(entry => entry.isFeatured = true || entry)
.filter(entry => entry.something == true))
.then((entries:EntryType[]) => someCallThatReturnsNothingButHasToBeDoneThere() || entries)
.then((entries:EntryType[]) => console.log(entries))
would be easier to read than: 比以下内容更容易阅读:
SomeCallThatReturnsAPromise
.then((entries:EntryType[]) => entries
.map(entry => {
entry.isFeatured = true;
return entry;
})
.filter(entry => entry.something == true))
.then((entries:EntryType[]) => {
someCallThatReturnsNothingButHasToBeDoneThere();
return entries;
})
.then((entries:EntryType[]) => console.log(entries))
Notes: 笔记:
1) I try to avoid creating a function for that. 1)我尽量避免为此创建函数。 My question was motivated by curiosity and just concerns what Vanilla ES6 or 7 syntaxes have to offer.
我的问题是出于好奇心,仅涉及Vanilla ES6或7语法提供的内容。
2) I was answered to use .forEach
rather than .map
. 2)有人回答我使用
.forEach
而不是.map
。 I design my code with a functional approach (hence the importance of compact callbacks), so .forEach
is not necessarily a good choice for me (and apparently it doesn't have advantages over map in terms of performance or memory consumption). 我使用功能性方法设计代码(因此,紧凑回调的重要性),因此
.forEach
不一定对我来说是一个不错的选择(显然,就性能或内存消耗而言, .forEach
不优于map)。 A one-line syntax is convenient both when handling promises callbacks or chains of array functions... 当处理承诺回调或数组函数链时,单行语法很方便。
3) the returned type when using the OR operator is a union type, EntryType|null
. 3)使用OR运算符时返回的类型是联合类型
EntryType|null
。 So it breaks the typing for the subsequent calls and implies a type assertion: 因此,它破坏了后续调用的类型,并暗含了类型断言:
SomeCallThatReturnsAPromise()
.then((entries:EntryType[]) => entries
.map(entry => (entry.isFeatured = true || entry) as EntryType)
.filter(entry => entry.something == true))
.then((entries:EntryType[]) => (someCallThatReturnsNothingButHasToBeDoneThere() || entries) as EntryType[])
.then((entries:EntryType[]) => console.log(entries))
That's getting heavier... I still don't know if I'll use that or stick with the two lines including the return statement. 这变得越来越重...我仍然不知道该使用还是坚持使用包括return语句的两行。
4) That's a reduced example. 4)这是一个简化的例子。 I know that my first
then
contains a synchronous call or that my example could be more accurate. 我知道,我的第一
then
包含一个同步调用,或者我的例子可能更准确。
entries.forEach( (entry) => entry.isFeatured = true );
No need to define the function separately. 无需单独定义功能。
Furthermore, as your elements are objects and are handled by reference, one can replace map()
by forEach()
, which removes the necessity to return a value. 此外,由于您的元素是对象并由引用处理,因此可以用
forEach()
替换map()
forEach()
,从而无需返回值。 (using map()
you would end up with two arrays consisting of the same elements, which probably is not, what you need) (使用
map()
,最终将得到两个包含相同元素的数组,可能不是您需要的)
You can do what @Sirko wrote but return it like so: 您可以执行@Sirko编写的操作,但可以这样返回:
SomeCallThatReturnsAPromise.then(entries => entries.forEach(entry => entry.isFeatured = true) || entries)
There's no need to use map
, instead using forEach
will give you a "one-liner", but then you want to return the same value that you received, using logical or ( ||
) you can do that. 不需要使用
map
,而是使用forEach
将为您提供“单线”,但是您想使用逻辑或( ||
)返回您收到的相同值,就可以这样做。
although @Sirko is right and, in that specific case, forEach
has more sense than using map
I think the OP was asking a generic question. 尽管@Sirko是正确的,在这种情况下,
forEach
比使用map
更有意义,我认为OP在问一个通用的问题。
So, in general, how do you assign a property and then return the whole object? 因此,通常来说,如何分配属性然后返回整个对象? this is my suggestion:
这是我的建议:
function mutateObject(element, value) { return (element.prop = value) && element || element } var object = {prop:1} var returned = mutateObject(object, 2) console.log(returned)
How does it work? 它是如何工作的? the first part
(element.prop = value)
assigns the value
to the property and return the value
to the expression. 第一部分
(element.prop = value)
分配value
给属性和返回value
来表达。
If the value returned is falsy, the value of the ||
如果返回的值是伪造的,则
||
的值 clause is returned. 子句被返回。 If it's truthy the value of the
&&
is returned. 如果是事实,则返回
&&
的值。
In this case we return the element
itself both times to be sure that's the object it will always be returned, no matter what we set in the property. 在这种情况下,无论我们在属性中设置了什么,我们都会两次都返回
element
本身,以确保始终会返回该对象。
Another way to write that is (element.prop = value) ? element : element
另一种写方法是
(element.prop = value) ? element : element
(element.prop = value) ? element : element
but, with this syntax, it looks more like a typo with the assignment instead of the comparison so I like the other syntax better. (element.prop = value) ? element : element
但是使用这种语法,它看起来更像是带有赋值的错字而不是比较,因此我更喜欢其他语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.