Home > database >  Remove array of arrays from array of arrays google sheets script/ javascript
Remove array of arrays from array of arrays google sheets script/ javascript

Time:09-06

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

CodePudding user response:

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>

  • Related