0

I am creating a google script to grab multiple rows from multiple sheets put the columns in to and array of arrays and, grab existing and do the same, I then merge both arrays together and delete duplicates:

  var existingItems = getSKUdata()
  var newItems = getPurchasedata()
  var mergedData = existingData
  for (item of newItems) {
    mergedData.push(item)
  }
  const mergedCleaned = Array.from(new Set(mergedData.map(JSON.stringify)), JSON.parse)

then remove any the arrays from 'existingItems' out of 'margedCleaned'. so I can import the newly added items without having any duplicates.

Current arrays:

newItems = [
   ['AAA-AAA','AAAA', '01.00'],
   ['BBB-BBB','BBBB', '02.00'],
   ['CCC-CCC','CCCC', '03.00'],
   ['DDD-DDD','DDDD', '04.00']
];

existingItems = [
   ['AAA-AAA','AAAA', '01.00'],
   ['BBB-BBB','BBBB', '02.00'],
];

I want to 'newItems' to have all the 'existingItems' removed so I can write the new arrays/ rows to my google sheets document.

newItems= [
   ['CCC-CCC','CCCC', '03.00']
   ['DDD-DDD','DDDD', '04.00']
];

I have tried multiple different snippets from other questions but nothing seems to work it doesn't remove the arrays. Only can use core-javascript

1
  • I want to try remove the existing data from the array before pushing the data to help speed it up, or wouldn't it make much of a difference speed wise? Commented Sep 5, 2022 at 21:39

1 Answer 1

1

Use Set and filter using the joint array values as keys:

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ 
const newItems = [
    ['AAA-AAA', 'AAAA', '01.00'],
    ['BBB-BBB', 'BBBB', '02.00'],
    ['CCC-CCC', 'CCCC', '03.00'],
    ['DDD-DDD', 'DDDD', '04.00'],
  ],
  existingItems = [
    ['AAA-AAA', 'AAAA', '01.00'],
    ['BBB-BBB', 'BBBB', '02.00'],
  ],
  existingItemsSet = new Set(existingItems.map((row) => row.join('⚅'))),
  filteredNewItems = newItems.filter((row) =>
    !existingItemsSet.has(row.join('⚅'))
  );
console.log(filteredNewItems);
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

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

4 Comments

Apologies, I forgot to add after I've grabbed existing and new data I'm merging them and deleting duplicates so its just logging a blank array: var mergedData = existingItems for (item of newItem) { mergedData.push(item) } const mergedCleaned = Array.from(new Set(mergedData.map(JSON.stringify)), JSON.parse)
@BlvckSZN So​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ?
When I run your script with it, it returns a blank array. when I remove the part where I remove duplicated items in the array it works fine
@BlvckSZN If you're still having trouble, ask a new question with sample data. Your latest modification to the question is unclear.

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.