finding an item by a property value

Started by blayde64, June 30, 2023, 13:02:46 PM

Previous topic - Next topic

blayde64

Hi,

I wrote a function that works - but is very slow and inefficient, as i am running a large model. i pass it an element type, a property name, and the value i am searching for - so for example i may have an business-actor, with a property named "Managers Email", and i may be searching for the first element with that is an actor with the specific property value. It returns an object if one exists, or null if nothing exists.

is there a way i could search for this without having to iterate through all my objects of a specific type? I wasnt sure i understood the prop documentation. Thanks for any help...

function findElementByProperty(elementType, propertyName, propertyValue) {
    var found = false;
    var obj;
   
    $(elementType).each(function (o) {
        if (o.prop(propertyName) == propertyValue) {
            found = true;
            obj = o;
        }
    }
    );

    if (!found)
        return null;
    else
        return obj;
}

Phil Beauvoir

Hi, the following is basically a shorter version of what you already have, but I don't know if it will be any quicker on a large model:

function findElementByProperty(elementType, propertyName, propertyValue) {
    let elements = $(elementType).filter(function(e) {
        return e.prop(propertyName) == propertyValue;
    })

    return elements.first();
}

Regards,

Phil
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Jean-Baptiste Sarrodie

Hi,

Quote from: blayde64 on June 30, 2023, 13:02:46 PMis there a way i could search for this without having to iterate through all my objects of a specific type?


Why use a script for something you can do with built-in features?

Simply use the search tool in the model tree, unselect the search by name, select the concept type you want than the propety name and value you're looking for.

See User Guide starting at p17: https://www.archimatetool.com/downloads/archi/Archi%20User%20Guide.pdf

Regards,

JB
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.