如何创建一个对象数组,其中一个属性是来自单个数组的数组

发布于 2025-02-11 18:30:27 字数 4141 浏览 1 评论 0原文

我得到了这个数组:

[
name1, 
[email protected],
[email protected],
[email protected],
name2,
[email protected],
name3,
name4,
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
]

从那个数组中,我需要对其进行解析并创建一个如下:

[
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}
]

示例:

[
  HR,
  [email protected],
  [email protected],
  [email protected],
  cyber,
  [email protected],
  [email protected],
  accessibility,
  accounting,
   [email protected],
   [email protected]
]

输出:

[
{ dlName: HR , members: [[email protected],[email protected],[email protected],]}, 
{ dlName: cyber , members:[[email protected],[email protected],]}, 
{ dlName: accesibility , members: []},
{ dlName: accounting , members: [[email protected],[email protected]]}
]

如何获得此输出,我尝试过的一切都不适合我。

提前致谢

I got this array:

[
name1, 
[email protected],
[email protected],
[email protected],
name2,
[email protected],
name3,
name4,
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
]

From that array I need to parse it and create an array like the following:

[
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}
]

example:

[
  HR,
  [email protected],
  [email protected],
  [email protected],
  cyber,
  [email protected],
  [email protected],
  accessibility,
  accounting,
   [email protected],
   [email protected]
]

output:

[
{ dlName: HR , members: [[email protected],[email protected],[email protected],]}, 
{ dlName: cyber , members:[[email protected],[email protected],]}, 
{ dlName: accesibility , members: []},
{ dlName: accounting , members: [[email protected],[email protected]]}
]

How can I get this output, everything I tried does not work for me needs.

Thanks in advance

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

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

发布评论

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

评论(1

默嘫て 2025-02-18 18:30:28

您可以通过查看字符串来减少阵列。

如果您获得了'@'将字符串添加到成员的最后一个对象,则其他sose将新对象添加到结果集中。

const
    data = ['name1', '[email protected]', '[email protected]', '[email protected]', 'name2', '[email protected]', 'name3', 'name4', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
    result = data.reduce((r, v) => {
        if (v.includes('@')) r[r.length - 1].members.push(v);
        else r.push(({ dlName: v, members: [] }));
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could reduce the array by having a look to the strings.

If you got an '@' add the string to the last object to members, otherose add a new object to the result set.

const
    data = ['name1', '[email protected]', '[email protected]', '[email protected]', 'name2', '[email protected]', 'name3', 'name4', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
    result = data.reduce((r, v) => {
        if (v.includes('@')) r[r.length - 1].members.push(v);
        else r.push(({ dlName: v, members: [] }));
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文