Best answer

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

  • 3 January 2023
  • 5 replies
  • 92 views

Userlevel 1

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

 

 

 

icon

Best answer by Todd Harper 3 January 2023, 23:35

View original

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

Userlevel 6
Badge +8

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];

Userlevel 7
Badge +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

Userlevel 1

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.

Userlevel 6
Badge +8

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

 

Userlevel 1

@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!