Subtract an array from another array of unequal length

340
8
Jump to solution
3 weeks ago
Shubhreet
Occasional Contributor

Hello,

does anyone knows how to subtract and array from another array of unequal length.  In other words, I am trying to remove a subset from an array.

For example, let array1 = [1, 3 , 5 ,6] and array2 =[3, 6] and the desired result = array1 - array2 = [1, 5]. 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You can use the Filter function

var array1 = [1,3,5,6]
var array2 = [3,6]
function exists(i){
  return !Includes(array2, i)
}
return Filter(array1, exists)

 

View solution in original post

8 Replies
KenBuja
MVP Esteemed Contributor

You can use the Filter function

var array1 = [1,3,5,6]
var array2 = [3,6]
function exists(i){
  return !Includes(array2, i)
}
return Filter(array1, exists)

 

Shubhreet
Occasional Contributor

Thank you @KenBuja for providing the right solution. I have been able to return the desired result.

I am further looking to convert this filtered array 1 into dictionary that could be displayed on dashboard table. But I am not able to console (array1) after 'return Filter (array1, exists)' which might be the reason I am not getting an output from the dictionary. 

Would you mind suggesting here something?  

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Glad to help you find the solution. Don't forget to click the "Accept as Solution" button.

As for your followup question, the script ends after the return on line 6, so anything you add after that won't be run. If you want to add a console statement after running the filter, you can do something like this

var array1 = [1,3,5,6]
var array2 = [3,6]
function exists(i){
  return !Includes(array2, i)
}
var newArray = Filter(array1, exists)
console(array1)
return newArray

To convert the array into a dictionary that can be used in a table, you have to use code like this:

var array1 = [1,3,5,6]
var array2 = [3,6]
function exists(i){
  return !Includes(array2, i)
}
var newArray = Filter(array1, exists)

var features = [];

for (var i in newArray) {
  console(newArray[i])
  Push(features, {
    attributes: {
      value: newArray[i]
    }
  }); 
}

var theDict = {
  fields: [{name: 'value', type: 'esriFieldTypeInteger'}],
  geometryType: '',
  features: features
}

return FeatureSet(theDict);
0 Kudos
Shubhreet
Occasional Contributor

Thank you so much for your response @KenBuja . I had actually build the code snippet for dictionary variable but I missed the for loop to iterate through newArray. 

With the code you shared, I have been able to get the right output through console within the for loop but the dictionary is showing null in the output table. Would you happen to know what might be going wrong? 

0 Kudos
KenBuja
MVP Esteemed Contributor

Can you post your code?

0 Kudos
Shubhreet
Occasional Contributor

Sure @KenBuja , here is the code snippet. I have only copied the part of code around from where the issue is coming from. Rest of the initial part of the code is working for me as I cross checked from console() function. My array 1 in the code below is 'output_yes' and array 2 is 'output_undesired'. Both my arrays have all the fields I mentioned in the 'Dicti' variable. Please note that console(Dicti) is giving the output as desired. 

 
 
function exists(x){
  return !Includes(output_undesired, x)
}
var finalArray = Filter(output_yes, exists);
//console(count(output_yes));
//return finalArray;


var features = [];

for (var a in finalArray) {
  //console(finalArray[a])
  Push(features, {
    attributes: {
        value: finalArray[a]
    }
  });
}

 

var Dicti = {
  fields: [
    {alias: "FacilityName", name: "FacilityName", type: "esriFieldTypeString"},
    {alias: "DateandTimeofSubmission", name: "DateTimeofSubmission", type: "esriFieldTypeDate"},
    {alias:"RecentUpdate", name: "RecentUpdate", type:"esriFieldTypeDate"},
    {alias: "CompleteOutbreakName", name:"CompleteOutbreakName", type: "esriFieldTypeString"},
    {alias: "dateoutbreakdeclared", name: "dateoutbreakdeclared", type:"esriFieldTypeDate" },
  ],
  geometryType: "esriGeometryPoint",
  features: features
};

//console(Dicti);
return FeatureSet(Dicti);

 

 

0 Kudos
KenBuja
MVP Esteemed Contributor

So this is a much different question than you originally asked. Are you trying to filter finalFeatures? Do the arrays 'output_yes' and 'output_undesired' refer to one of the fields in that FeatureSet?

0 Kudos
Shubhreet
Occasional Contributor

yes @KenBuja , I did not wanted to post this complicated code thinking I might be able to implement the solution from simple sample code. I am trying to remove all values/attributes contained in output_undesired from output_yes. 

My output_yes array has following value:

 

{"attributes":{"FacilityName":"Concordia Place","CompleteOutbreakName":"Respiratory Covid-19","DateTimeofSubmission":"2023-08-03T18:27:00.000Z","RecentUpdate":"2023-08-03T18:27:00.000Z","OutbreakStatus":"Yes","ObjID":75}},

 

{"attributes":{"FacilityName":"Dauphin Personal Care Home","CompleteOutbreakName":"Gastrointestinal Norovirus","DateTimeofSubmission":"2023-08-01T23:13:00.000Z","RecentUpdate":"2023-08-01T23:13:00.000Z","OutbreakStatus":"Yes","ObjID":70}},

 

{"attributes":{"FacilityName":"Heritage Lodge","CompleteOutbreakName":"Respiratory Other","DateTimeofSubmission":"2023-08-03T18:41:00.000Z","RecentUpdate":"2023-08-03T18:41:00.000Z","OutbreakStatus":"Yes","ObjID":78}},

 

{"attributes":{"FacilityName":"Minnedosa PCH","CompleteOutbreakName":"Gastrointestinal E. coli","DateTimeofSubmission":"2023-08-03T18:43:00.000Z","RecentUpdate":"2023-08-03T18:43:00.000Z","OutbreakStatus":"Yes","ObjID":79}},

 

{"attributes":{"FacilityName":"Minnedosa PCH","CompleteOutbreakName":"Respiratory Covid-19","DateTimeofSubmission":"2023-07-31T18:19:00.000Z","RecentUpdate":"2023-07-31T18:19:00.000Z","OutbreakStatus":"Yes","ObjID":60}},

 

 

{"attributes":{"FacilityName":"Pinaow Wachi Inc.","CompleteOutbreakName":"Gastrointestinal Norovirus","DateTimeofSubmission":"2023-11-01T17:44:00.000Z","RecentUpdate":"2023-11-01T17:44:00.000Z","OutbreakStatus":"Yes","ObjID":90}},

 

{"attributes":{"FacilityName":"Pinaow Wachi Inc.","CompleteOutbreakName":"Other Scabies","DateTimeofSubmission":"2023-07-25T18:23:00.000Z","RecentUpdate":"2023-07-25T18:23:00.000Z","OutbreakStatus":"Yes","ObjID":65}},

 

 

{"attributes":{"FacilityName":"Salem Home Inc","CompleteOutbreakName":"Multidrug-Resistant Organism (MDRO) C. auris ","DateTimeofSubmission":"2024-04-16T18:59:00.000Z","RecentUpdate":"2024-04-16T18:59:00.000Z","OutbreakStatus":"Yes","ObjID":106}}]

 

 

My output_undesired array has following value:

 

 

[{"attributes":{"FacilityName":"Dauphin Personal Care Home","CompleteOutbreakName":"Gastrointestinal Norovirus","DateTimeofSubmission":"2023-08-01T23:13:00.000Z","RecentUpdate":"2023-08-01T23:13:00.000Z","OutbreakStatus":"Yes","ObjID":70}},

 

{"attributes":{"FacilityName":"Minnedosa PCH","CompleteOutbreakName":"Respiratory Covid-19","DateTimeofSubmission":"2023-07-31T18:19:00.000Z","RecentUpdate":"2023-07-31T18:19:00.000Z","OutbreakStatus":"Yes","ObjID":60}},

 

{"attributes":{"FacilityName":"Pinaow Wachi Inc.","CompleteOutbreakName":"Other Scabies","DateTimeofSubmission":"2023-07-25T18:23:00.000Z","RecentUpdate":"2023-07-25T18:23:00.000Z","OutbreakStatus":"Yes","ObjID":65}}]

 

0 Kudos