nerdfisch: DevBits

Kleine, aber feine Code-Snippets, nützliche Tweaks und elegante Lösungsansätze aus dem Entwickler-Alltag

13.11.2025 | Michael Ebert

How to translate menu link URIs in Drupal

menus
translation
multi-language
18.06.2024 | Dominik Wille

Prevent destination URLs from being indexed by search engines

robots.txt
Disallow: /*?destination=
Disallow: /*&destination=
robots.txt
SEO
copy-paste
18.06.2024 | Pascal Crott

Load an object from another object by value of a certain key

getObjectFromDataByKeyValue.js
/**
 * Loads an object from another object by value of a certain key.
 *
 * @usage 
 *   data = [
 *     'foo': {
 *         'id' => 5,
 *         'label' => 'Foo'
 *     },
 *     'bar': {
 *         'id' => 6,
 *         'label' => 'Bar'
 *     }
 *   ]
 *   foo = getObjectFromDataByKeyValue('id', 5, data)
 *
 * @param key
 *   The key we are comparing our value with.
 * @param value
 *   The value the key should have.
 * @param data
 *   The data you want to search in.
 * @returns {unknown}
 */
function getObjectFromDataByKeyValue(key, value, data) {
  return Object.values(data).find(o => o[key] === value)
}
js
objects