0

I have input like this. I tried some solutions. But it doesn't work. I need to merge same invoice_nr objects to one array. Also I need other objects another array. All arrays must be another array.

const result = [
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'red'
  },
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'yellow'
  },
  {
    invoice_nr: 17,
    order_id: 5574,
    color: 'green'
  },
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'yellow'
  },
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'blue'
  }
];

But, I need output like this. How can I achieve that in javascript? Array must be like this.

const result = [
  [
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'red'
  },
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'yellow'
  }
  ],
  [
  {
    invoice_nr: 17,
    order_id: 5574,
    color: 'green'
  }
  ],
  [
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'yellow'
  },
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'blue'
  }
  ]
];
1
  • Please edit your question to put what you have tired, the output and the desired output and your specific challenge with the code you post. Commented Dec 2, 2022 at 13:27

3 Answers 3

4

You can use .reduce() to build a lookup object where key is the invoice_nr and value is an array. In every iteration look for a key is already exists in lookup object if it is then push to the existing list, if it's not add a new property in the lookup object.

const result = [ { invoice_nr: 16, order_id: 5577, color: 'red' }, { invoice_nr: 16, order_id: 5577, color: 'yellow' }, { invoice_nr: 17, order_id: 5574, color: 'green' }, { invoice_nr: 18, order_id: 5578, color: 'yellow' }, { invoice_nr: 18, order_id: 5578, color: 'blue' } ];

const res = result.reduce((a,b) => ((a[b.invoice_nr] ??= []).push(b),a),{});
console.log(Object.values(res));
.as-console-wrapper { max-height: 100% !important }

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

Comments

0

If they're in order, then I'd reduce like this:

result.reduce((arr, item) => {
    const group = arr.at(-1), prevItem = group?.at(-1);
    if (prevItem?.invoice_nr === item.invoice_nr) {
        group.push(item);
    } else {
        arr.push([item]);
    }
    return arr;
}, []);

Comments

0

You have to at a first get array of unique ID and use it for filter you data.

const result = [
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'red'
  },
  {
    invoice_nr: 16,
    order_id: 5577,
    color: 'yellow'
  },
  {
    invoice_nr: 17,
    order_id: 5574,
    color: 'green'
  },
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'yellow'
  },
  {
    invoice_nr: 18,
    order_id: 5578,
    color: 'blue'
  }
];

let uniq = [];
let res = [];
// get unique ID
result.forEach(i => !uniq.includes(i.invoice_nr) ? uniq.push(i.invoice_nr) : '' );
// filter by ID
uniq.forEach(id => {
    res.push(result.filter(o => o.invoice_nr === id))
})
// that you need in res
console.log(res);

1 Comment

Welcome to Stackoverflow. Make your answer more useful by explaining why the code will fix the problem that was described in the question

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.