How can I filter a string array object property in typescript function?

I’m working on a typescript function repository, and I’m trying to filter an object set based on values in an String Array property. This property should exactly match all values stored in an array passed as input.

Pseudo-code:
if (input_array) {
obj = obj.filter(obj => Filters.and(…input_array.map(x => obj.stringArrayProperty.exactMatch(x) ))
}

If input_array = [‘abc’, ‘xyz’], I want in output only object where obj.stringArrayProperty = [‘abc’, ‘xyz’].

Unfortunately, it seems that it’s not possible to have an exactMatch on an array. I tried with .contains() method but it doesn’t work as expected since I’d have in output also objects like [‘abc’, ‘xyz’, ‘other’] and it won’t be correct.

Thanks!

Hi!

In TypeScript, you can use the filter() method.

If you have an array of objects, and each object has a string array property. You want to filter the items in those string arrays based on some condition. Here’s a basic example:

// This should be your ontology type
interface MyObject {  
  id: number;
  tags: string[];
}

const myArray: MyObject[] = [
  { id: 1, tags: ['typescript', 'javascript', 'nodejs'] },
  { id: 2, tags: ['python', 'typescript', 'react'] },
  { id: 3, tags: ['java', 'angular', 'typescript'] },
];

// Function to filter out specific tags (for example, keeping only "typescript")
function filterTags(objects: MyObject[], tagToFilter: string): MyObject[] {
  return objects.map(obj => ({
    ...obj,
    tags: obj.tags.filter(tag => tag === tagToFilter),
  }));
}

const filteredArray = filterTags(myArray, 'typescript');

console.log(filteredArray);

Explanation:

  • Interface MyObject: This defines the shape of each object in the array, having an id and a tags array (which is an array of strings).
  • The filterTags function: This function takes an array of objects and a tag to filter. For each object in the array, it uses map() to create a new array where the tags property is filtered using the filter() method, keeping only the tags that match the tagToFilter.
  • filteredArray: After running the filterTags function, you get a new array of objects where the tags array contains only the filtered values.

Output:

[
  { id: 1, tags: ['typescript'] },
  { id: 2, tags: ['typescript'] },
  { id: 3, tags: ['typescript'] }
]

Hope this helps

Thanks for your reply, but I don’t think it solves my problem.
I cannot use filter method since I got this error: "Property ‘filter’ does not exist on type ‘IStringArrayPropertyFilter’ ".
The ‘IStringArrayPropertyFilter’ is the string array ontology object property that needs to be equal to the input array.
In output I want only the objects where stringArrayProperty matches exactly with input array.
If my input array is [“abc”, “xyz”], and my input object is the following:

ID stringArrayProperty
1 [“abc”, “xyz”, “1”, “2”]
2 [“abc”, “xyz”]
3 [“abc”, “xyz”, “4”]

I’d like to have in output only the following object:

ID stringArrayProperty
2 [“abc”, “xyz”]

Hope it clarifies the need! Thanks

Use this instead:

// Helper function to check if two arrays are exactly equal
function arraysEqual(arr1: string[], arr2: string[]): boolean {
  if (arr1.length !== arr2.length) return false;
  return arr1.every((value, index) => value === arr2[index]);
}

// Function to filter objects with an exact tags match
function filterExactMatch(objects: MyObject[], tagArray: string[]): MyObject[] {
  return objects.filter(obj => arraysEqual(obj.tags, tagArray));
}

const tagArrayToMatch = ['typescript', 'javascript', 'nodejs'];

const filteredArray = filterExactMatch(myArray, tagArrayToMatch);

In my case I have the following:

const a = objectType.filter(ot =>
            ot.stringListProperty.matchesAnyToken('srting')
        );

but you can use also matchAllTokens()

const a = objectType.filter(ot =>
            ot.stringListProperty.matchesAllTokens(stringArrayVariable)
        );

Keep in mind order won’t be enforced.