1

I'm trying to merge a set of strings in an array based on a series of matching properties and remove the items that were duplicates.

My current array:

{id: "12", value: "Option 1"},
{id: "55", value: "Option A"},
{id: "55", value: "Option B"},
{id: "55", value: "Option C"},
{id: "55", value: "Option D"},
{id: "106", value: "Option 1"}

I want to merge all items with the id of '55' so my array will look like this:

{id: "12", value: "Option 1"},
{id: "55", value: "Option A, Option B, Option C, Option D"},
{id: "106", value: "Option}

I can only manage to merge the first 2 items using a for loop and checking the -1 sibling and can't work out to do this without serious bloat to my code.

Any advice would be appreciated.

2
  • Can you post your for loop code. Commented Nov 2, 2017 at 18:45
  • I was using almost exactly what Usman Rana posted (nested for loop) but I forgot the condition i != j and was getting irregularities. Commented Nov 3, 2017 at 7:21

3 Answers 3

2

You may use an object as a lookup table while storing the results in an array in parallel:

const lookup = {}, result = [];

for( const {id, value} of input ){
  if(lookup[id]){
     lookup[id].value += "," + value;
  }else{
     result.push( lookup[id] = {id, value} );
  }
}

This is quite new javascript, but it works on modern engines ;)

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

Comments

1

A bit late may be. But you can try this with for/while loop. It's a very basic approach. And if time complexity is not a concern. Otherwise Jons w's answer is good.

var x = [{id: "12", value: "Option 1"},
{id: "55", value: "Option A"},
{id: "55", value: "Option B"},
{id: "55", value: "Option C"},
{id: "55", value: "Option D"},
{id: "12", value: "Option D"},
{id: "53", value: "Option D"},
{id: "106", value: "Option 1"}];

for(var j =0; j<x.length;j++){
for(var i =0; i<x.length;i++){
  if(x[j].id == x[i].id && i !=j){
     x[j].value = x[j].value + "," +x[i].value;
     x.splice(i, 1);
     }
  }
}
console.log(x);

2 Comments

Thanks Usman, I was trying a for loop with nested in a for loop but forgot about the i != j and was getting problems. I accepted the top answer as it's quite a small snippet of code but this is what I would have gone for originally.
linting, linting, linting... And you need to decrement i after splicing... But still: thanks ;)
0

You can use array#reduce.

const data = [{id: "12", value: "Option 1"},{id: "55", value: "Option A"},{id: "55", value: "Option B"},{id: "55", value: "Option C"},{id: "55", value: "Option D"},{id: "106", value: "Option 1"}];

var result = data.reduce((r, o) => {
  return r[o.id] = r[o.id] ? (r[o.id].value += ", " + o.value, r[o.id]) : o, r;
},{})

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.