0

This is an input array of object

var array= [
    { name : "c", arr: "d" },
    { name : "a", arr: "b" },
    { name : "c", arr: "e" },
    { name : "a", arr: "b" },
    { name : "c", arr: "d" }
];

result should be like

var result =[
     { name : "c", arr: "d,e" },
     { name : "a", arr: "b" }
]
2
  • @CertainPerformance look proper to question this differs from as you mentioned Commented Aug 29, 2019 at 8:22
  • if I have no idea how can I post my effort dude @CertainPerformance Commented Aug 29, 2019 at 8:26

1 Answer 1

3

Use reduce, a Set to store the unique letters, then join them together with map at the end:

var array= [
    { name : "c", arr: "d" },
    { name : "a", arr: "b" },
    { name : "c", arr: "e" },
    { name : "a", arr: "b" },
    { name : "c", arr: "d" }
];

const res = Object.values(array.reduce((a, { name, arr }) => {
  a[name] = a[name] || { name, arr: new Set() };
  a[name].arr.add(arr);
  return a;
}, {})).map(({ name, arr }) => ({ name, arr: [...arr].join(",")}));

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.