0

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?

2
  • 1
    The problem is when you push the entire array onto uniqueArr, you can't just use indexOf on it any more because it no longer contains simple strings. Commented May 28, 2015 at 12:00
  • I removed jQuery tag because this is unrelevant here Commented May 28, 2015 at 12:02

3 Answers 3

4

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

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

Comments

0

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

Comments

0

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>

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.