简体   繁体   中英

Remove duplicates from array of arrays by the first object

I have an array of arrays which looks like this:

arr = [
["Bob","USA","55"],
["Frank","Canada","20"],
["Bob","UK","35"],
["Bob","France","38"],
["Anna","Poland","22"]
]

I like to remove duplicate arrays which have the same value on the first position(the same name) - so I'd like to my output will look like that:

arr = [
["Bob","USA","55"],
["Frank","Canada","20"],
["Anna","Poland","22"]
]

I'm trying to do this in this way:

uniqueArr = []
for (var i in arr) {
if (uniqueArr.indexOf(arr[i][0]) === -1)) {
uniqueArr.push(arr[i][0])
}

Everything works ok - my output looks like Bob, Frank, Anna

But the problem is when I'm trying to recive whole arrays with unique value name. When I'm doing:

uniqueArr = []
for (var i in arr) {
if (uniqueArr.indexOf(arr[i][0]) === -1)) {
uniqueArr.push(arr[i])
}

My output looks exactly like the input array. Do you know where I'm doing wrong?

You could keep track of the key string in a separate array and track that instead, for example:

var uniqueArr = [],
    keys = []; // Create an array for storing the key values
for (var i in arr) {    
    if (keys.indexOf(arr[i][0]) === -1) {
        uniqueArr.push(arr[i]); // Push the value onto the unique array
        keys.push(arr[i][0]); // Push the key onto the 'key' array
    }
}
console.log(uniqueArr);

jsFiddle example

try

arr = [
    ["Bob", "USA", "55"],
    ["Frank", "Canada", "20"],
    ["Bob", "UK", "35"],
    ["Bob", "France", "38"],
    ["Anna", "Poland", "22"]
]
var newArr = [];
for (var i = 0; i < arr.length; i++) {
    if (isInArr(newArr, arr[i]) == -1) {
        newArr.push(arr[i]);
    }
}

function isInArr(checkArr, value) {
    var index = -1;
    for (var i = 0; i < checkArr.length; i++) {
        if (checkArr[i][0] == value[0]) {
            index = i;
            break;
        }
    }
    return index;
}

console.log(newArr)

DEMO

Take a look atthe function unique2. It works!

 originalArr = [ ["Bob", "USA", "55"], ["Frank", "Canada", "20"], ["Bob", "UK", "35"], ["Bob", "France", "38"], ["Anna", "Poland", "22"] ]; function unique(arr) { uniqueArr = []; for (var i in arr) { if (uniqueArr.indexOf(arr[i][0]) === -1) { uniqueArr.push(arr[i]); } } return uniqueArr; } function unique2(arr) { uniqueArr = []; keys = []; for (var i in arr) { if (keys.indexOf(arr[i][0]) === -1) { uniqueArr.push(arr[i]); keys.push(arr[i][0]); } } return uniqueArr; } var response = document.getElementById('response'); response.textContent = JSON.stringify(unique(originalArr)); var response2 = document.getElementById('response2'); response2.textContent = JSON.stringify(unique2(originalArr)); 
 <h1>Your code</h1> <div id="response"></div> <h1>My code</h1> <div id="response2"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM