小编ggo*_*len的帖子

Java日期格式化程序

我得到"YYYY-mm-dd hh:mm"格式化对象的日期格式.

如何格式化输入格式化程序对象才能获得"YYYY-mm-dd";

java simpledateformat

9
推荐指数
2
解决办法
8万
查看次数

JavaScript 嵌套对象结构中的递归树搜索

我试图弄清楚如何在这个 JSON 对象中递归搜索节点。我尝试过一些东西但无法得到它:

var tree = {
    "id": 1,
    "label": "A",
    "child": [
        {
            "id": 2,
            "label": "B",
            "child": [
                {
                    "id": 5,
                    "label": "E",
                    "child": []
                },
                {
                    "id": 6,
                    "label": "F",
                    "child": []
                },
                {
                    "id": 7,
                    "label": "G",
                    "child": []
                }
            ]
        },
        {
            "id": 3,
            "label": "C",
            "child": []
        },
        {
            "id": 4,
            "label": "D",
            "child": [
                {
                    "id": 8,
                    "label": "H",
                    "child": []
                },
                {
                    "id": 9,
                    "label": "I",
                    "child": []
                }
            ]
        }
    ] …
Run Code Online (Sandbox Code Playgroud)

javascript tree recursion json

9
推荐指数
1
解决办法
1万
查看次数

在Perl中将字符串拆分为数组

my $line = "file1.gz file2.gz file3.gz";
my @abc = split('', $line);
print "@abc\n";
Run Code Online (Sandbox Code Playgroud)

预期产量:

file1.gz
file2.gz
file3.gz
Run Code Online (Sandbox Code Playgroud)

我希望输出file1.gz$abc[0],file2.gz$abc[1]file3.gz$abc[2].我怎么拆分$line

arrays string perl split

8
推荐指数
2
解决办法
11万
查看次数

无法读取null属性进行破坏

销毁很酷,但是在销毁嵌套对象时我开始遇到严重的问题。我有以下代码:

const {
      credit: { amont },
    } = userProfile
Run Code Online (Sandbox Code Playgroud)

这很危险,因为如果信用为空怎么办?整个应用中断了。我该如何预防?我知道一种方法是使用Typescript,但我宁愿不这样做。我开始怀疑对嵌套的破坏与使用点没有什么区别。

javascript destructuring ecmascript-6 reactjs

8
推荐指数
1
解决办法
2059
查看次数

单击时将类添加到 HTML 列表中的一个且仅一个元素

我有一个这样的清单:

 <ul>
   <li class="item">Item 1</li>
   <li class="item">Item 2</li>
   <li class="item">Item 3</li>
 </ul>
Run Code Online (Sandbox Code Playgroud)

我需要将活动类分别添加到每个项目中,并在单击时将其从上一个项目中删除。

类似于这个问题Vanilla JS remove class from previous selection但是由于旧的浏览器兼容性,我需要使用 For 循环而不是 ForEach 。

我一直在努力使这个答案适应我的例子:

const items = document.querySelectorAll(".item");

for (let i = 0; i < items.length; i++) {
  const item = items[i];
  item.addEventListener("click", addActiveClass);

  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    item.addEventListener("click", removeClass);
  }
}

function removeClass(e) {
  e.target.classList.remove("active");
}

function addActiveClass(e) {
  e.target.classList.add("active");
}
Run Code Online (Sandbox Code Playgroud)

但它仍然没有按预期工作:(

javascript foreach for-loop

8
推荐指数
2
解决办法
580
查看次数

如何使用 Playwright 使用选择器查找框架 (iframe)

我有一个小问题,无法找到使用 Microsoft Playwright 框架的答案。根据文档,您可以使用以下代码获取 iframe:

const frame = page.frame('frame-login');
Run Code Online (Sandbox Code Playgroud)

但是如何使用选择器来查找 iframe 并与之交互?我需要使用 CSS 选择器来查找我的 iframe,因为它没有 id。

任何帮助表示赞赏

javascript node.js playwright

8
推荐指数
2
解决办法
2万
查看次数

“getByText”和“getByTestId”有什么区别?在测试库/反应中

getByText和 和有什么区别getByTestId

当我测试 React 组件时,这两个功能之间存在一些差距。

在代码中测试失败getByText,但在getByTestId.

我有代码,当单击某个元素时,标题的颜色变为红色。

为什么会有这样的差别呢?

Container我省略了和的样式组件Content。这具有“切换”道具以将颜色更改为红色。

这是getByText代码:

const { getByText } = render(<ListPresenter content={ListText} />);
const colorChangedText = getByText(/the party/);
fireEvent.click(colorChangedText);
screen.debug(); // The result of render I want !
expect(colorChangedText).toHaveStyle("color: red");    * failed
Run Code Online (Sandbox Code Playgroud)

这是getByTestId代码:

const { getByText } = render(<ListPresenter content={ListText} />);
fireEvent.click(getAllByTestId("list-element-toggle")[0]);
screen.debug(); // The result of render I want !
const colorChangedText = getAllByTestId("list-element-content")[0];
expect(colorChangedText).toHaveStyle("color: red");   * success
Run Code Online (Sandbox Code Playgroud)

这是渲染的组件:

const …
Run Code Online (Sandbox Code Playgroud)

testing reactjs styled-components react-testing-library

8
推荐指数
1
解决办法
3万
查看次数

2 个列表的最大路径总和

我的问题是关于Codewars 上的这个型。该函数采用两个具有不同元素的排序列表作为参数。这些列表可能有也可能没有共同的项目。任务是找到最大路径和。在查找总和时,如果有任何常见项目,您可以选择更改到其他列表的路径。

给出的例子是这样的:

list1 = [0, 2, 3, 7, 10, 12]
list2 = [1, 5, 7, 8]
0->2->3->7->10->12 => 34
0->2->3->7->8      => 20
1->5->7->8         => 21
1->5->7->10->12    => 35 (maximum path)
Run Code Online (Sandbox Code Playgroud)

我解决了 kata,但我的代码不符合性能标准,因此执行超时。我能为它做些什么呢?

这是我的解决方案:

def max_sum_path(l1:list, l2:list):
    common_items = list(set(l1).intersection(l2))
    if not common_items:
        return max(sum(l1), sum(l2))
    common_items.sort()
    s = 0
    new_start1 = 0
    new_start2 = 0
    s1 = 0
    s2 = 0
    for item in common_items:
        s1 = sum(itertools.islice(l1, new_start1, l1.index(item)))
        s2 = sum(itertools.islice(l2, new_start2, l2.index(item)))
        new_start1 = …
Run Code Online (Sandbox Code Playgroud)

python algorithm list python-3.x

8
推荐指数
1
解决办法
704
查看次数

如何使用getaddrinfo使用外部IP连接到服务器?

我正在编写一个小型的C客户端/服务器应用程序,但在使用外部IP地址时无法使连接正常工作.客户端和服务器的代码都来自这里,特别是客户端:

    char *default_server_name = "localhost";
    char *server_name = NULL;
    int nport = DEFAULT_DAMA_PORT;
    char port[15];

    // Parse the command line options
    if (parse_options(argc, argv, &server_name, &nport) < 0) {
        return -1;
    }

    if (server_name == NULL) {
        server_name = default_server_name;
    }

    snprintf(port, 15, "%d", nport);

    // Connect to the server
    int client_socket;
    struct addrinfo hints, *servinfo, *p;
    int rv;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(server_name, port, &hints, &servinfo)) != 0) { …
Run Code Online (Sandbox Code Playgroud)

c sockets getaddrinfo

7
推荐指数
1
解决办法
2万
查看次数

如何安全地运行不受信任的 python 代码?

这是这样的场景,我的网站有一些由网站用户生成的不安全代码,可以在我的服务器上运行。

我想禁用 python 的一些保留字以保护我的运行环境,例如evalexecprint

有没有一种简单的方法(不改变python解释器,我的python版本是2.7.10)来实现我之前描述的功能?

python sandbox

7
推荐指数
2
解决办法
2290
查看次数