0

I am trying to get unique values from two arrays which looks like that:

array[{A,B,C},{C,D,E},{1,3,2},....]

both looks the same.

I tried to add them using concat and the get unique values from looping.

So I ended up with this:

    function uniqueValues() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
        var srcSheet = ss.getSheetByName("arr1");
       const array1 = srcSheet.getRange(1, 1, srcSheet.getLastRow(), srcSheet.getLastColumn()).getValues();
       var srcSheet1 = ss.getSheetByName("arr2");
        const array2 = srcSheet1.getRange(1, 1, srcSheet1.getLastRow(), srcSheet1.getLastColumn()).getValues();
 
var dodaj = array1.concat(array2);
  
  
for (var i=0; i<dodaj.length; i++) {
    var listI = dodaj[i];
    loopJ: for (var j=0; j<dodaj.length; j++) {
        var listJ = dodaj[j];
        if (listI === listJ) continue; 
        for (var k=listJ.length; k>=0; k--) {
            if (listJ[k] !== listI[k]) continue loopJ;
        }
        
        dodaj.splice(j, 1);
    }
}
  
  var result = ss.getSheetByName("test").getRange(2, 5, dodaj.length, 3).setValues(dodaj);
  //Logger.log(dodaj);
}

It was working well when array looked like this array[{A,B},{C,D}] but with three elements it started to return duplicates as well... I have no idea what can be wrong.

2
  • I think that to provide the sample input and output values you expect will help users understand your current issue of script and think of the solution. Commented Nov 16, 2020 at 8:32
  • At the beginning You are talking about 2 arrays of values and then You provide as an input 1 array array[{A,B,C},{C,D,E},{1,3,2},....] . Please update the question with valid input and expected output. Commented Nov 16, 2020 at 8:36

1 Answer 1

1

If I understand you correctly, you want to retrieve the unique rows from the values in arr1 and arr2. That is to say, you want to remove duplicate inner arrays from dodaj.

After using concat to merge the two arrays, you could do the following:

  • Use JSON.stringify() to transform each inner array to a string, in order to compare them without iterating through them.
  • Use the Set constructor and the spread syntax in order to remove the duplicate strings (see this answer).
  • Transform the strings back to arrays with JSON.parse().

Code snippet:

var dodaj = array1.concat(array2);
dodaj = [...new Set(dodaj.map(JSON.stringify))].map(JSON.parse);
var result = ss.getSheetByName("test").getRange(2, 5, dodaj.length, dodaj[0].length).setValues(dodaj);
Sign up to request clarification or add additional context in comments.

1 Comment

works perfect! I didn't know that it could be solved with just one line. Thanks a lot and sorry if I was not so specific about my goal.

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.