Skip to content

Instantly share code, notes, and snippets.

@fobiasmog
Last active March 23, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fobiasmog/5b48e0a750655a6c487fd439e30bb30c to your computer and use it in GitHub Desktop.
Save fobiasmog/5b48e0a750655a6c487fd439e30bb30c to your computer and use it in GitHub Desktop.
Заметки по JS

Рекурсивное получение массива значений в хэше/дереве

import flatMapDeep from 'lodash/flatMapDeep'

function getResponseErrors(errors) {

  function deep(n) {
    if (typeof(n) === 'object') return flatMapDeep(n, deep);
    return n;
}
 
  return flatMapDeep(errors, deep);
}

Get element by XPath

function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

Random value on range

Math.floor(Math.random() * (max - min) + min)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment