【问题标题】:How to remove TextRow and add a string to JSON in NodeJs如何在 NodeJs 中删除 TextRow 并将字符串添加到 JSON
【发布时间】:2020-08-07 15:24:57
【问题描述】:

我想删除 TextRow 并向 NodeJs 中的 JSON 添加一个字符串(true)。我已经在我的代码下面添加了。

NodeJs 代码:

function groupBy(objectArray, property) {
  return objectArray.reduce(function (acc, obj) {
    let key = obj[property]
    if (!acc[key]) {
      acc[key] = []
    }
    acc[key].push(obj)
    return acc
  }, {})
}

group数据:

[
  TextRow { name: '/products', email: '111@gmail.com' },
  TextRow { name: '/products', email: '222@gmail.com' },
  TextRow { name: '/sales', email: '111@gmail.com' },
  TextRow { name: '/sales', email: '222@gmail.com' },
  TextRow { name: '/sales', email: '333@gmail.com' },
  TextRow { name: '/sales', email: '444@gmail.com' },
  TextRow { name: '/finance', email: '333@gmail.com' },
  TextRow { name: '/finance', email: '444@gmail.com' },
]

我的输出:

{
  '/products': [
    TextRow { name: '/products', email: '111@gmail.com' },
    TextRow { name: '/products', email: '222@gmail.com' },
  ],
  '/sales': [
    TextRow { name: '/products', email: '111@gmail.com' },
    TextRow { name: '/products', email: '222@gmail.com' },
    TextRow { name: '/products', email: '333@gmail.com' },
    TextRow { name: '/products', email: '444@gmail.com' },
  ],
  '/products': [
    TextRow { name: '/products', email: '333@gmail.com' },
    TextRow { name: '/products', email: '444@gmail.com' },
  ],
}

输出应该是:

{
  '/products': [
    {
      '111@gmail.com': true,
      '222@gmail.com': true,
    }
  ],
  '/sales': [
    {
      '111@gmail.com': true,
      '222@gmail.com': true,
      '333@gmail.com': true,
      '444@gmail.com': true,
    }
  ],
  '/finance': [
    {
      '333@gmail.com': true,
      '444@gmail.com': true,
    }
  ]
}

【问题讨论】:

    标签: javascript node.js arrays json


    【解决方案1】:

    您想要创建一个新对象,而不是推动整行。我不太清楚为什么您的最终输出是一个包含单个对象的数组,或者为什么每封电子邮件都有一个 true

    const key = obj[property];
    if (!acc[key]) {
      acc[key] = [{}];
    }
    acc[key][0][obj.email] = true;
    return acc;
    

    这样做会产生一个对象,其键是name,值是一个数组,其中一个对象的键是电子邮件地址。

    【讨论】:

    • 很高兴它对您有用。如果可以接受,请标记为答案。我仍然不清楚你为什么要创建一个内部只有一个对象的数组。
    【解决方案2】:

    这是一个关于你应该如何做的例子。它会给你预期的结果

    const list = [{
        TextRow: {
          name: '/products',
          email: '111@gmail.com'
        }
      },
      {
        TextRow: {
          name: '/products',
          email: '222@gmail.com'
        }
      }, {
        TextRow: {
          name: '/sales',
          email: '111@gmail.com'
        }
      }, {
        TextRow: {
          name: '/sales',
          email: '222@gmail.com'
        }
      }, {
        TextRow: {
          name: '/sales',
          email: '333@gmail.com'
        }
      }, {
        TextRow: {
          name: '/sales',
          email: '444@gmail.com'
        }
      }, {
        TextRow: {
          name: '/finance',
          email: '333@gmail.com'
        }
      }, {
        TextRow: {
          name: '/finance',
          email: '444@gmail.com'
        }
      },
    ]
    
    const result = list.reduce((acc, x) => {
      const name = x['TextRow']['name'];
      const obj = {
        [x['TextRow'].email]: true
      };
      if (acc[name]) {
        acc[name].push(obj)
      } else {
        acc[name] = [obj];
      }
      return acc;
    
    }, [])
    
    console.log(result)

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多