How to prevent same key and document ID appear multiple times in index?

My document is like this:

{
id: ‘ABC123’,
background: ‘red’,
foreground: ‘black’,
border: ‘black’
}

I need to be able to find documents by color. So, I create this view:

function (doc, meta) {
    	emit(doc.background, null);
    	emit(doc.foreground, null);
    	emit(doc.border, null);
}

The index now becomes:

‘red’ - ‘ABC123’
‘black’ - ‘ABC123’
‘black’ - ‘ABC123’

If I search the index by key ‘black’, I get the same document twice. This is completely unhelpful. How to make sure that the same key and document ID is added only once to the index? More importantly, how do I solve my use case? Thanks.

Never mind. I came up with a solution moments after posting the question. Basically, in the map function, make sure that you emit one key only once per document.

function (doc, meta) { var list = new Array();
    emit(doc.background, null); 
    list.push(doc.background);

    if (list.indexOf(doc.foreground) == -1) {
            emit(doc.foreground, null);
            list.push(doc.foreground);
    }
    if (list.indexOf(doc.border) == -1) {
            emit(doc.border, null);
    }

}