2

I have found questions that kind of touch on the issue I'm having, but I haven't found a solution that works for me yet. I have this array: [[1, red], [2, green], [3, red], [3, blue], [5, green]] and I need it to return [[1, red], [2, green], [3, blue]. What I need the code to do is go through the array and find ONLY colors that match, not numbers, and get rid of that entire index.

I have tried something like this

var uniqueArray = colors.filter(function(item, pos) {
return colors.indexOf(item) == pos;
});

I'm thinking that this code is searching for a complete match, and I only require a partial match. So basically, how would I modify .filter() to get rid of partial duplicates (only matching the colors)?

Please let me know if I need to provide any more information.

1
  • your question is a bit confusing. Why do you need the 1, 2 and 3 in the result you're searching for? Could these not be determined as answer.indexOf('blue') + 1 or something of the like? Commented Sep 23, 2016 at 21:49

4 Answers 4

1
// Parameter marr: multidimensional array
function removeSameColors(marr){
    var carr = [];
    var rarr = [];
    var j = -1;

    for(var i = 0, l = marr.length; i < l; i++){
        if(carr[marr[i][1]] !== true){
            carr[marr[i][1]] = true;
            rarr[++j] = marr[i];
        }
    }

    return rarr;
}

That should solve your problem with very low execution time.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use a hash table with the color and use Array#filter for the wanted items.

var data = [[1, 'red'], [2, 'green'], [3, 'red'], [3, 'blue'], [5, 'green']],
    result = data.filter(function (a) {
        if (!this[a[1]]) {
            return this[a[1]] = true;                    
        }
    }, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

I would use a for loop to populate a new, unique array:

var old_array = [[1, red], [2, green], [3, red], [3, blue], [5, green]],
    old_count = old_array.length,
    unique_array = [], check_array = [], i = 0;

for(; i < old_count; i++) {
    if (check_array.indexOf(old_array[i][1]) === -1) {
        check_array.push(old_array[i][1]);// this array is filled with new colors, and used for checking
        unique_array.push(old_array[i]);
    }
}

2 Comments

I ran this on fiddle, and the output was an empty array jsfiddle.net/sxrapxrv/1
My apologies, I had 1 mistake I didn't catch. I updated my code and now it works fine...see fiddle here ---> jsfiddle.net/sxrapxrv/2
0

I would just keep track of the unique colors in an object passed into the filter, because as a hash it's guaranteed to be unique. If the object doesn't have a property by that color name, it returns it from the filter. Otherwise if it does it ignores the item.

var colors = [[1, "red"], [2, "green"], [3, "red"], [3, "blue"], [5, "green"]];

var uniqueArray = colors.filter(function(item, pos) {
    if (!this.hasOwnProperty(item[1])) {
        return this[item[1]] = true;
    }
    return false;
}, {});

This gives you uniqueArray = [[1,"red"],[2,"green"],[3,"blue"]], as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.