SearchKey

Chercher une clé dans un objet JS en mode strict ou include sur différents niveaux de profondeur.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/578734/1828936/SearchKey.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

/*
* Skip les valeurs falsy (null, undefined, "", 0, false) et continue la recherche.
* 
* @param {Object} target - L'objet dans lequel chercher
* @param {string} searchedKey - Le nom de clé à chercher
* @param {boolean} strictName - true = nom exact, false = includes (défaut: true)
* @param {number} maxDepth - Profondeur de recherche, 1 = niveau courant seulement (défaut: 1)
* 
* @example
*   searchKey(state, 'search_key_2');              // nom exact, niveau courant
*   searchKey(cache, ':_key_', false);             // includes, niveau courant
*   searchKey(state, ':_key_', false, 20);         // includes, recherche profonde
*   searchKey(state, 'search_key_2', true, 20);    // nom exact, recherche profonde
*/

function searchKey(target, searchedKey, strictName = true, maxDepth = 1) {
    if (!target || typeof target !== 'object' || maxDepth <= 0) return null;
    
    // Niveau courant : on cherche une clé qui matche
    const matchingKey = Object.keys(target).find(key => (strictName ? key === searchedKey : key.includes(searchedKey)) && target[key]);

    if (matchingKey) return target[matchingKey];

    // Pas trouvé au niveau courant, on descend si maxDepth le permet
    for (const value of Object.values(target)) {
        const result = searchKey(value, searchedKey, strictName, maxDepth - 1);
        if (result !== null) return result;
    }
    
    return null;
}