递归循环数组以创建对象

发布于 2025-01-11 17:26:38 字数 290 浏览 0 评论 0原文

我正在努力解决这个算法。我知道解决方案将是递归的,但我对如何解决这个问题感到困惑。请随意尝试一下。谢谢

问题:['a','b','c',['d','e','f',['g','h']],[i, j, k]]

Output:

{
  a: true,
  b: true,
  c: true,
  d: {
   e: true,
   f: true,
   g: {
     h: true
   }
  },
   i: {
     j: true,
     k: true
   }
}

I'm seriously struggling to solve this algorithm. I know the solution will be recursive, but i'm stumped on how to solve this problem. Feel free to take a crack at it. Thank you

Problem: ['a','b','c',['d','e','f',['g','h']], [i, j, k]]

Output:

{
  a: true,
  b: true,
  c: true,
  d: {
   e: true,
   f: true,
   g: {
     h: true
   }
  },
   i: {
     j: true,
     k: true
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我一直都在从未离去 2025-01-18 17:26:38

您可以使用 Object.fromEntries 并为其提供键/值对。当“key”恰好是一个数组时,使用第一个元素作为key,对数组的其余部分进行递归以获得value部分:

const toObject = arr =>
    Object.fromEntries(arr.map(item => Array.isArray(item)
        ? [item[0], toObject(item.slice(1))]
        : [item, true]
    ));

const arr = ["a", "b", "c", ["d", "e", "f", ["g", "h"]], ["i", "j", "k"]];
const result = toObject(arr);
console.log(result);

You could use Object.fromEntries and feed it key/value pairs. When the "key" happens to be an array, use the first element as key, and perform recursion on the remainder of the array to get the value part:

const toObject = arr =>
    Object.fromEntries(arr.map(item => Array.isArray(item)
        ? [item[0], toObject(item.slice(1))]
        : [item, true]
    ));

const arr = ["a", "b", "c", ["d", "e", "f", ["g", "h"]], ["i", "j", "k"]];
const result = toObject(arr);
console.log(result);

反差帅 2025-01-18 17:26:38

使用 Array#reduce

const convert = (arr = []) =>
  arr.reduce((acc, e) => {
    if (Array.isArray(e)) {
      const [k, ...sub] = e;
      acc[k] = convert(sub);
    } else {
      acc[e] = true;
    }
    return acc;
  }, {});

console.log(
  convert(["a", "b", "c", ["d", "e", "f", ["g", "h"]], ["i", "j", "k"]])
);

Using Array#reduce:

const convert = (arr = []) =>
  arr.reduce((acc, e) => {
    if (Array.isArray(e)) {
      const [k, ...sub] = e;
      acc[k] = convert(sub);
    } else {
      acc[e] = true;
    }
    return acc;
  }, {});

console.log(
  convert(["a", "b", "c", ["d", "e", "f", ["g", "h"]], ["i", "j", "k"]])
);

迟到的我 2025-01-18 17:26:38

在 Ruby 中(想想伪代码)。

def recurse(arr)
  arr.each_with_object({}) do |e,h|
    case e
    when String
      h[e.to_sym] = true
    else # Array
      h[e.first.to_sym] = recurse(e.drop(1))
    end
  end
end

假设(与相关示例略有不同)

arr = ['a','b','c',['d','e','f',['g','h', ['m', 'n']]], ['i', 'j', 'k']]

然后

recurse arr
  #=> {
  #     :a=>true,
  #     :b=>true,
  #     :c=>true,
  #     :d=> {
  #       :e=>true,
  #       :f=>true,
  #       :g=>{
  #       :h=>true,
  #         :m=>{
  #         :n=>true
  #       }
  #     }
  #   },
  #   :i=>{
  #     :j=>true,
  #     :k=>true
  #   }
  # }

In Ruby (think pseudo-code).

def recurse(arr)
  arr.each_with_object({}) do |e,h|
    case e
    when String
      h[e.to_sym] = true
    else # Array
      h[e.first.to_sym] = recurse(e.drop(1))
    end
  end
end

Suppose (slightly changed from example in question)

arr = ['a','b','c',['d','e','f',['g','h', ['m', 'n']]], ['i', 'j', 'k']]

Then

recurse arr
  #=> {
  #     :a=>true,
  #     :b=>true,
  #     :c=>true,
  #     :d=> {
  #       :e=>true,
  #       :f=>true,
  #       :g=>{
  #       :h=>true,
  #         :m=>{
  #         :n=>true
  #       }
  #     }
  #   },
  #   :i=>{
  #     :j=>true,
  #     :k=>true
  #   }
  # }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文