小编Mor*_*erg的帖子

从深度未知的嵌套数组中删除项目

我正在尝试根据正确匹配的数组从嵌套数组中删除项目。

适用三个要求:

  1. 阵列的深度未知。项目可以有嵌套的子项目。
  2. 仅应移除没有儿童的物品
  3. 如果这些项目不在匹配的数组中,则应将其删除

我构建了一个函数来递归地到达最深层次并根据 $match 数组过滤项目。

这是我的代码到目前为止的样子:

import * as lodash from "https://cdn.skypack.dev/lodash@4.17.21";

let filterRecursively = (arr, match) => {
  // Recursively go to the deepest array we can find
  arr.forEach(el => {
      arr = el.children ? filterRecursively(el.children, match) : arr
  });

  // If we are at the deepest level we filter the items ...
  if (arr[0] && arr[0].children === undefined) {
    return _.filter(arr, (item) => {
        return match.includes(item.name)
    })
  } else { // ... if not we …
Run Code Online (Sandbox Code Playgroud)

javascript arrays recursion nested lodash

5
推荐指数
1
解决办法
816
查看次数

使用 Laravel Inertia 观察路线变化

使用新的 Inertia Laravel 堆栈 \xe2\x80\x93\xc2\xa0 如何观察路线变化?

\n

我正在尝试做一些非常简单的事情,例如:

\n
watch: {\n  route () {\n    console.log('You changed route')\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我一直无法找到这方面的任何信息。

\n

inertiajs laravel vue.js

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

标签 统计

arrays ×1

inertiajs ×1

javascript ×1

laravel ×1

lodash ×1

nested ×1

recursion ×1

vue.js ×1