Most Popular

1500 questions
3897
votes
12 answers

Proper use cases for Android UserManager.isUserAGoat()?

I was looking at the new APIs introduced in Android 4.2. While looking at the UserManager class I came across the following method: public boolean isUserAGoat() Used to determine whether the user making this call is subject to…
Ovidiu Latcu
  • 70,421
  • 14
  • 74
  • 84
3896
votes
48 answers

How do I test for an empty JavaScript object?

After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case?
falmp
  • 39,377
  • 3
  • 20
  • 18
3890
votes
15 answers

Iterating over dictionaries using 'for' loops

d = {'x': 1, 'y': 2, 'z': 3} for key in d: print(key, 'corresponds to', d[key]) How does Python recognize that it needs only to read the key from the dictionary? Is key a special keyword, or is it simply a variable?
TopChef
  • 40,537
  • 10
  • 26
  • 27
3884
votes
35 answers

Delete commits from a branch in Git

I would like to know how to delete a commit. By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch. I read git help, and I think the command I should use is git…
hap497
  • 141,763
  • 43
  • 79
  • 93
3853
votes
19 answers

Undoing a git rebase

Does anybody know how to easily undo a git rebase? The only way that comes to mind is to go at it manually: git checkout the commit parent to both of the branches then create a temp branch from there cherry-pick all commits by hand replace the…
webmat
  • 54,646
  • 12
  • 53
  • 59
3845
votes
39 answers

What is the !! (not not) operator in JavaScript?

I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does? The context in which I saw this was, this.vertical = vertical !== undefined ?…
Hexagon Theory
  • 41,001
  • 5
  • 25
  • 30
3840
votes
23 answers

Make an existing Git branch track a remote branch?

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch? I know I can just edit the .git/config file, but it seems there should be an easier way.
Pat Notz
  • 198,042
  • 30
  • 88
  • 92
3837
votes
79 answers

How can I validate an email address using a regular expression?

Over the years I have slowly developed a regular expression that validates most email addresses correctly, assuming they don't use an IP address as the server part. I use it in several PHP programs, and it works most of the time. However, from time…
acrosman
  • 12,604
  • 10
  • 38
  • 55
3805
votes
25 answers

How to insert an item into an array at a specific index (JavaScript)

I am looking for a JavaScript array insert method, in the style of: arr.insert(index, item) Preferably in jQuery, but any JavaScript implementation will do at this point.
tags2k
  • 76,900
  • 31
  • 76
  • 105
3790
votes
33 answers

Is there a CSS parent selector?

How do I select the
  • element that is a direct parent of the anchor element? As an example, my CSS would be something like this: li < a.active { property: value; } Obviously there are ways of doing this with JavaScript, but I'm hoping that…
  • jcuenod
    • 52,209
    • 14
    • 61
    • 102
    3787
    votes
    11 answers

    Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

    Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for…
    3769
    votes
    46 answers

    How do I efficiently iterate over each entry in a Java Map?

    If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation…
    iMack
    • 36,591
    • 3
    • 20
    • 19
    3750
    votes
    20 answers

    How do I tell if a regular file does not exist in Bash?

    I've used the following script to see if a file exists: #!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi What's the correct syntax to use if I only want to check if the file…
    Bill the Lizard
    • 386,424
    • 207
    • 554
    • 861
    3726
    votes
    44 answers

    Loop through an array in JavaScript

    In Java, you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can I do the same in JavaScript?
    Mark Szymanski
    • 53,532
    • 23
    • 67
    • 87
    3715
    votes
    31 answers

    Checking if a key exists in a JavaScript object?

    How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
    Adam Ernst
    • 49,494
    • 18
    • 56
    • 71