将 Json 数据映射到新的键值对 JavaScript 对象

Nih*_*har 5 javascript arrays mapping json key-value

我想将一个 json 数据映射到一个新的 javascript 对象,如下所示。这里的 json 数据是动态的,可以有更多的文件和更多的用户。群组信息是新的,依赖于父子信息。有人可以帮我吗?感谢您的时间。

前:

{
  "userinfo": {
    "/home/user/main/sub/info/1stfile.txt": {
      "John": "something",
      "Mike": "something",
      "Merry": "something",
      "Susan": "something"
    },
    "/home/user/main/info/2ndfile.txt": {
      "Mulan": "something",
      "James": "something"
    },
    "/home/user/main/info/3rdfile.txt": {
      "Nancy": "something"
    },
    "/home/user/main/4thfile.txt": {
      "Kamal": "something",
      "Xian": "something",
      "Mila": "something"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

后:

{
  "name": "main",
  "children": [
    {
      "name": "1stfile",
      "children": [
        {
          "name": "John",
          "group": "1"
        },
        {
          "name": "Mike",
          "group": "1"
        },
        {
          "name": "Merry",
          "group": "1"
        },
        {
          "name": "Susan",
          "group": "1"
        }
      ],
      "group": 1
    },
    {
      "name": "2ndfile",
      "children": [
        {
          "name": "Mulan",
          "group": 2
        },
        {
          "name": "James",
          "group": 2
        }
      ],
      "group": 2
    },
    {
      "name": "3rdfile",
      "children": [
        {
          "name": "Nancy",
          "group": 3
        }
      ],
      "group": 3
    },
    {
      "name": "4thfile",
      "children": [
        {
          "name": "Kamal",
          "group": 4
        },
        {
          "name": "Xian",
          "group": 4
        },
        {
          "name": "Mila",
          "group": 4
        }
      ],
      "group": 4
    }
  ],
  "group": 0
}
Run Code Online (Sandbox Code Playgroud)

我试图使用以下代码构建一个父子块

{
  "userinfo": {
    "/home/user/main/sub/info/1stfile.txt": {
      "John": "something",
      "Mike": "something",
      "Merry": "something",
      "Susan": "something"
    },
    "/home/user/main/info/2ndfile.txt": {
      "Mulan": "something",
      "James": "something"
    },
    "/home/user/main/info/3rdfile.txt": {
      "Nancy": "something"
    },
    "/home/user/main/4thfile.txt": {
      "Kamal": "something",
      "Xian": "something",
      "Mila": "something"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

给出以下输出

{
  0: {
    "name": "/home/user/main/sub/info/1stfile.txt",
    "group": 1
  },
  1: {
    "name": "/home/user/main/info/2ndfile.txt",
    "group": 1
  },
  2: {
    "name": "/home/user/main/info/3rdfile.txt",
    "group": 1
  },
  3: {
    "name": "/home/user/main/4thfile.txt",
    "group": 1
  }
}
Run Code Online (Sandbox Code Playgroud)

该值分配正确,但正在创建额外的键(0,1,2,3)!

jun*_*n-k 2

假设你需要这样的东西。

您可以利用Array.map()Object.keys()函数进行操作。

<script>
  const beforeJSON = `{
    "userinfo": {
      "/home/user/main/sub/info/1stfile.txt": {
        "John": "something",
        "Mike": "something",
        "Merry": "something",
        "Susan": "something"
      },
      "/home/user/main/info/2ndfile.txt": {
        "Mulan": "something",
        "James": "something"
      },
      "/home/user/main/info/3rdfile.txt": {
        "Nancy": "something"
      },
      "/home/user/main/4thfile.txt": {
        "Kamal": "something",
        "Xian": "something",
        "Mila": "something"
      }
    }
  }`
  const before = JSON.parse(beforeJSON);
  const filenames = Object.keys(before.userinfo);

  const after = {
    name: 'main',
    children: [],
    group: 0,
  }

  const children = filenames.map((filename, idx) => {
    const innerChildren = Object.keys(before.userinfo[filename]).map((n) => ({
      name: n,
      group: idx + 1,
    }))
    return ({
      name: filename,
      children: innerChildren,
      group: idx + 1,
    });
  })

  after.children = children;

  console.log(after);
</script>
Run Code Online (Sandbox Code Playgroud)

请下次在发布另一个问题之前格式化您的代码。