javascript

Array

Check whether array empty

if (array !=== undefined && array.length != 0){}
  • Or, no need to check whether array is undefined, just use null conditional operator.
if (array?.length){}

forEach

array.forEach( () => {} )

filter

remove element that fits the criteria marked in {}

array.filter( () => {} )

Check whether an array contains all the elements in another array (tag check)

required_tags = ['A', 'B']
doc_tags = ['A', 'B', 'C']

if (requried_tags.every((tag) => doc_tags.includes(tag)))

sort

return negative value if a comes before b

array.sort((a, b) => {
    return a - b
}

Javascript object vs JSON

Unlike JSON, in JS, we don’t need to wrap object keys in double quotes.

🌐Check out other language versions!
網站使用 Hugo 製作
模板 StackJimmy 設計