Skip to main content
Best answer

Create 2 arrays from 1. One array with Unique Items, and the second with occurrence of those items


Hello,

I have a list of text items that I would like to loop through to create 2 outputs. One array that contains the unique items in the list, and another that contains the occurrences of those items.

 

I am trying to create an Invoice in Quickbooks and I want the invoice to have the item and the quantity, instead of a duplicate item over and over again with a quantity of 1.

 

Thanks in advance!

 

Kind regards,

Josh

 

 

 

Best answer by Todd HarperBest answer by Todd Harper

Hi @Joshua Mursic! Would you please include a screenshot of a sample of the original array? Or confirm that I’m understanding it correctly with the example below?

var origList = ["item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 2", "item 2", "item 3", "item 3", "item 3", "item 3"];

Where the desired output would be:

itemList = [item 1, item 2, item 3];

qtys = [7, 2, 4];

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Todd Harper
Forum|alt.badge.img+8
  • Zapier Expert
  • 197 replies
  • Answer
  • January 3, 2023

Hi @Joshua Mursic! Would you please include a screenshot of a sample of the original array? Or confirm that I’m understanding it correctly with the example below?

var origList = ["item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 2", "item 2", "item 3", "item 3", "item 3", "item 3"];

Where the desired output would be:

itemList = [item 1, item 2, item 3];

qtys = [7, 2, 4];


Troy Tessalone
Forum|alt.badge.img+14

Hi @Joshua Mursic 

Good question.

Check out some of the related Code Mode topics that may provide guidance: https://community.zapier.com/search?q=Code+Mode&search_type=tag


Todd Harper wrote:

Hi @Joshua Mursic! Would you please include a screenshot of a sample of the original array? Or confirm that I’m understanding it correctly with the example below?

var origList = ["item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 2", "item 2", "item 3", "item 3", "item 3", "item 3"];

Where the desired output would be:

itemList = [item 1, item 2, item 3];

qtys = [7, 2, 4];

Hey Todd!

Thanks for the help!

Yes this is exactly correct.


Todd Harper
Forum|alt.badge.img+8
  • Zapier Expert
  • 197 replies
  • January 3, 2023

@Joshua Mursic Awesome! This should do it for you :)

const arr = ["item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 2", "item 2", "item 3", "item 3", "item 3", "item 3"]; // Obviously, replace this with the actual array from your inputData

const items = [];
const qtys = [];

for (var i = 0; i < arr.length; i++) {
  if (items.includes(arr[i])) {
    qtys[items.indexOf(arr[i])] += 1;
  } else {
    items.push(arr[i]);
    qtys.push(1);
  }
}

output = [{items, qtys}];

And this is what the result looks like when placing it into a QBO create invoice step:

 


Todd Harper wrote:

@Joshua Mursic Awesome! This should do it for you :)

const arr = ["item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 1", "item 2", "item 2", "item 3", "item 3", "item 3", "item 3"]; // Obviously, replace this with the actual array from your inputData

const items = [];
const qtys = [];

for (var i = 0; i < arr.length; i++) {
  if (items.includes(arr[i])) {
    qtys[items.indexOf(arr[i])] += 1;
  } else {
    items.push(arr[i]);
    qtys.push(1);
  }
}

output = [{items, qtys}];

And this is what the result looks like when placing it into a QBO create invoice step:

 

Amazing! Thanks so much!