在clojure中,我可以像这样构造一个地图:
(let [{:keys [key1 key2]} {:key1 1 :key2 2}]
...)
Run Code Online (Sandbox Code Playgroud)
这与CoffeeScript的方法类似:
{key1, key2} = {key1: 1, key2: 2}
Run Code Online (Sandbox Code Playgroud)
CoffeeScript也可以这样做:
a = 1
b = 2
obj = {a, b} // just like writing {a: a, b: b}
Run Code Online (Sandbox Code Playgroud)
在Clojure中有这样的捷径吗?
最近我在研究Firefox Add-on Builder SDK源代码,并偶然发现了这样的常量声明:
const { getCodeForKey, toJSON } = require("../../keyboard/utils");
Run Code Online (Sandbox Code Playgroud)
我可以找到有关CommonJS模块的信息,但是这项任务的部分内容会让我感到困惑,因为它必须是特定于语言的,而且我无法对其进行任何操作.
有人能指出我的一些规范/草案,解释这里发生了什么?
javascript gecko spidermonkey destructuring variable-assignment
在下面的函数中,我得到了带有属性的textarea对象current.
这里,嵌套的解构与Start和End变量一起工作.但current变量不起作用.
function someFunction({ current: { selectionStart: Start, selectionEnd: End } }, AppStateSetter) {
// do something with current, Start, and End
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jest 对我的 React 应用程序中的一个功能组件进行 SnapShot 验证。这是使用 Jest 的组件和测试文件
import React, { useState } from 'react';
import useForm from 'react-hook-form';
import { useAppState } from 'Shared/store';
...
const Form = () => {
const {customReducer, dispatch} = useAppState();
const { register, handleSubmit, errors, reset } = useForm({});
//custom form methods
return (
<>
<Form ...>
...
</Form>
....
</>
);
export default Form;
Run Code Online (Sandbox Code Playgroud)
和我使用 Jest 的测试
import React from 'react';
import TestRenderer from 'react-test-renderer';
import ShallowRenderer from 'react-test-renderer/shallow';
import Form …Run Code Online (Sandbox Code Playgroud) 我似乎不记得如何编写这个解构模式,你能帮我吗?
假设我有一个这样的对象:
{
id: 'po_495743057439095',
object: 'payout',
amount: 18560,
arrival_date: 1576195200,
automatic: true,
balance_transaction: 'txn_32472309478903284',
created: 1575774863,
currency: 'eur',
description: 'STRIPE PAYOUT',
destination: 'ba_329047329048323',
failure_balance_transaction: null,
failure_code: null,
failure_message: null,
livemode: true,
metadata: {},
method: 'standard',
source_type: 'card',
statement_descriptor: null,
status: 'in_transit',
type: 'bank_account',
}
Run Code Online (Sandbox Code Playgroud)
(这实际上是来自 Stripe API 的一个对象)。
我想将这个对象推送到我的自定义数组中,但只保留 5 个字段。
我可以:
arr.push({
id: myObject.id,
object: myObject.object,
amount: myObject.amount,
arrival_date: myObject.arrival_date,
created: myObject.created
})
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有任何语法可以帮助我以更短的方式实现相同的目标。
基本上我很确定有一种语法可以做到这一点,只需要编写一次“myObject”一词,编写每个键名称一次,并且不创建临时变量,但我不记得如何实现。
我不是在询问编写特定的函数,我不需要帮助。我只是问是否有内置的解构语法可以做到这一点,因为我很确定有,但我找不到它,尽管之前搜索了相当长的时间。
编辑:
我正在寻找的答案就在这里:Elegant way to copy a part of an object
是否可以使以下代码在 Kotlin 中编译?
val variable: String? = "string"
val (a, b) = variable?.run {
1 to 2
}
Run Code Online (Sandbox Code Playgroud) 我有一个功能:
export function getSubjectsForStudent(data: any) : any[]
Run Code Online (Sandbox Code Playgroud)
我从外部源收到“数据参数”,定义强类型是不可行的。“return”源自“data”,因此它也是any类型。
“主”组件将“返回”传递给“子”组件,如下所示:
<MainCategories subjects={getSubjectsForStudent(data)} />
Run Code Online (Sandbox Code Playgroud)
在主要类别中
export default function MainCategories(props: any) {
const tmp = props.subjects;
...
Run Code Online (Sandbox Code Playgroud)
它有效并且没问题。
但我想
导出默认函数 MainCategories( {subjects} ) {
有人可以帮忙吗?
const auth = 'Bearer AUTHORIZATION_TOKEN'
const { groups: { token } } = /Bearer (?<token>[^ $]*)/.exec(auth)
console.log(token) // "AUTHORIZATION_TOKEN"
Run Code Online (Sandbox Code Playgroud)
当我在打字稿中尝试时,它无法编译,因为groups可能是null. 如果我放一个!after exec(...),它只会踢罐头:token可能是未定义的。
在打字稿中,有什么方法可以像上面那样解构正则表达式吗?
我这里有这个 useEffect 代码:
useEffect(() => {
if (status === "completed" && !error)
props.onAddedComment();
}, [status,error,props.onAddedComment]);
Run Code Online (Sandbox Code Playgroud)
但我在终端中收到此警告: React Hook useEffect 缺少依赖项:“props”。包含它或删除依赖项数组。但是,当任何prop 更改时,“props”也会更改,因此首选修复方法是在 useEffect 调用之外解构“props”对象,并引用 useEffect 内的那些特定 props
props.onAddedComment如果我传递的是而不是整个 props 对象,为什么需要使用解构?即使我添加了,它仍然会引用整个道具吗.onAddedComment?
params我对这段代码中使用 , 有同样的问题:
useEffect(() => {
sendRequest(params.quoteId);
}, [params.quoteId, sendRequest]);
Run Code Online (Sandbox Code Playgroud)
我在这里没有收到此警告,为什么?
简而言之,我的问题是,即使我在.somethingprops 之后添加,我是否应该始终使用解构,以及为什么它不使用参数警告我。
谢谢!
据我了解,JavaScript 数组可以通过执行以下操作根据特定索引进行解构。
const { 3: selectedByIndex } = arr;
Run Code Online (Sandbox Code Playgroud)
这会将索引 3 处的元素的值分配给变量selectedByIndex。
有没有办法传入变量索引值来解构数组?下面的方法不起作用,它似乎而是index在 上寻找属性arr。
const index = 3;
const arr = [1, 2, 3, 4, 5]
const { index: selectedByIndex } = arr;
Run Code Online (Sandbox Code Playgroud) destructuring ×10
javascript ×6
reactjs ×4
typescript ×2
arrays ×1
clojure ×1
coffeescript ×1
ecmascript-6 ×1
enzyme ×1
gecko ×1
jestjs ×1
kotlin ×1
nullable ×1
react-hooks ×1
react-props ×1
react-ref ×1
regex ×1
spidermonkey ×1