How to remove TextRow and add a string to JSON in NodeJs

Viewed 959

I want to remove TextRow and add a string(true) to JSON in NodeJs. I have added below my code.

NodeJs Code:

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 data :

[
  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' },
]

My output:

{
  '/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' },
  ],
}

Output Should be:

{
  '/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,
    }
  ]
}
2 Answers

Instead of pushing the entire row, you want to create a new object. I'm not quite sure why your final output is an array with a single object though or why there is a true for each email.

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

Doing something like this will result in an object whose keys are the name and the values are each an array with a single object whose keys are the email addresses.

Here is an example on how you should do it. It will give you the expected result

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)

Related