在Node.js中按属性过滤对象

每一个

我的对象看起来像这样:

    {
    "performance": {...},
    "a": "",
    "b": "N",
    "c": true,
    "groups": [
      {
        "acctTypes": [
          {
            "xxx": "xxx",
            "ttt": "ttt",
            "accounts": [
            {
              ...
            },
            {
              ...
            },
            {
              ...
            }
            ],
          },
          {
            "acctTypes": [
              {
                "xxx": "xxx",
                "ttt": "ttt",
                "accounts": [
                  {
                    ...
                  },
                  {
                    ...
                  },
                  {
                    ...
                  },
                  {
                    ...
                  },
                  {
                    ...
                  }
                ],
              },
    ],
    "summary": [
      {
        ...
      },
      {
        ...
      }
    ]
}

我正在尝试从中提取所有“帐户”(已查看_js和lodash-但找不到任何相关内容)

结果应该是包含所有帐户的对象/数组

任何帮助将不胜感激。

谢谢

埃维亚塔格

如果obj的结构采用类似的下层结构,则可能对您有用

obj = {
	"performance": {},
	"a": "",
	"b": "N",
	"c": true,
	"groups": [
	{
		"acctTypes": [
			{
				"xxx": "xxx",
				"ttt": "ttt",
				"accounts": [
					{
						a:1
					},
					{
						b:2
					}
				],
			},
			{
				"acctTypes": [
					{
						"xxx": "xxx",
						"ttt": "ttt",
						"accounts": [
							{
								c:3
							},
							{
								d:4
							}
						],
					},
				],
				"summary": [
					{

					},
					{
					}
				]
			}]}]}

let filtered = obj.groups.reduce(key => key.accounts).acctTypes
let accounts = []
filtered.map(key => {
	if (key.accounts) {
		accounts.push(...key.accounts)
	} else if (key.acctTypes){
		accounts.push(...key.acctTypes[0].accounts)
	}
})
console.log(accounts)

我已将数据添加到一些PoC帐户中。我测试其中的每个项目是否filtered包含一个accounts键,或者acctTypes其中一个应该包含一个accounts键,然后accounts使用ES6扩展将值添加到数组中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章