admin管理员组

文章数量:1400148

I have looked at other questions similar to this but haven't seen an answer that fits with what I am trying to do.

I have a multidimensional array:

var arr = [
["apple","ghana",15],
["apple","brazil",16],
["orange","nigeria",10],
["banana","ghana",6]
]

I want to filter the array by only looking at the first column that contains the fruit name and return an array of unique values. So it will look like this:

var uniqueArr = [
["apple","ghana",15],
["orange","nigeria",10],
["banana","ghana",6]
]

I would like to provide a function that will do this. I have tried:

    function isUnique (rows,index,self) {
      return self.indexOf(rows) === index
    }

But it didn't work. Any help would be appreciated!

I have looked at other questions similar to this but haven't seen an answer that fits with what I am trying to do.

I have a multidimensional array:

var arr = [
["apple","ghana",15],
["apple","brazil",16],
["orange","nigeria",10],
["banana","ghana",6]
]

I want to filter the array by only looking at the first column that contains the fruit name and return an array of unique values. So it will look like this:

var uniqueArr = [
["apple","ghana",15],
["orange","nigeria",10],
["banana","ghana",6]
]

I would like to provide a function that will do this. I have tried:

    function isUnique (rows,index,self) {
      return self.indexOf(rows) === index
    }

But it didn't work. Any help would be appreciated!

Share Improve this question asked Apr 14, 2020 at 15:56 Ajay UbhiAjay Ubhi 4093 gold badges7 silver badges15 bronze badges 3
  • what would be the deciding factor between choosing one apple item in the array vs selecting/returning another apple item in the array? – blurfus Commented Apr 14, 2020 at 15:58
  • There wouldn't be a deciding factor. That's why I used the indexOf function to just find the first result in the array. – Ajay Ubhi Commented Apr 14, 2020 at 16:00
  • 1 Gotcha, so the deciding factor is the first element found in the array ;) – blurfus Commented Apr 14, 2020 at 16:00
Add a ment  | 

4 Answers 4

Reset to default 6

Use reduce for getting the result.

var arr = [["apple","ghana",15],["apple","brazil",16],["orange","nigeria",10],["banana","bangladesh",20],["banana","ghana",6]];

const res = arr.reduce((a, c) => {
	if (!a.find(v => v[0] === c[0])) {
		a.push(c);
	}
	return a;
}, []);

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

You could take a Set and filter the array by a check if the value exists or not.

  • If exist reject the element.

  • If not, add the value to the set and take the element.

var array = [["apple", "ghana", 15], ["apple", "brazil", 16], ["orange", "nigeria", 10], ["banana", "ghana", 6]],
    seen = new Set,
    result = array.filter(([value]) => !seen.has(value) && seen.add(value));

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

Try this

const filtered = arr.filter((row, index) => arr.findIndex(row2 => row2[0] === row[0]) >= index);

Reduce the array to an object, using column 1 (index 0) as the key, and then convert back to an array with Object.values():

const arr = [["apple","ghana",15],["apple","brazil",16],["orange","nigeria",10],["banana","ghana",6]]

const result = Object.values(
  arr.reduce((r, a) => {
    if(!r[a[0]]) r[a[0]] = a // add the item to the object, if the property doesn't exist yet
  
    return r
  }, {})
)

console.log(result)

本文标签: javascriptHow to use FILTER on a multidimensional array to retrieve unique valuesStack Overflow