[
['a', 123, 'test'],
['a', 100, 'test'],
['rrr', 123, 'tt'],
['rrr', 9, 'tt'],
['b', 9, 'a'],
]
[123, 9]
const getDuplicates = (arr, key) => Array
.from(arr.reduce((acc, { [key]: n }) => acc.set(n, -~acc.get(n)), new Map))
.filter(n => n[1] > 1)
.map(n => n[0]);
const duplicates = getDuplicates(arr, 1);
const getDuplicates = (arr, key = n => n) => Array
.from(arr.reduce((acc, n) => (n = key(n), acc.set(n, acc.has(n))), new Map))
.reduce((acc, n) => (n[1] && acc.push(n[0]), acc), []);
const duplicates = getDuplicates(arr, n => n[1]);
const getDuplicates = arr =>
[...arr.reduce((acc, n) => (
acc[+acc[0].has(n)].add(n),
acc
), [ new Set, new Set ])[1]];
const duplicates = getDuplicates(arr.map(n => n[1]));
Не поможет ли lodash?
const duplicates = _(arr)
.map(n => n[1])
.groupBy()
.filter(n => n.length > 1)
.map(n => n[0])
.value();
function f(arr, n) { // Массив arr, по индексу n
let result = [];
let new_arr = [];
for (let i = 0; i < arr.length; i++) {
if (new_arr.includes(arr[i][n])) result.push(arr[i][n]);
new_arr.push(arr[i][n]);
}
return result;
}
Как это сделать коротко и красиво?Машинокод хотите?)) Вариант LJ322 хотя бы читаемый.
let a = [
['a', 123, 'test'],
['a', 100, 'test'],
['rrr', 123, 'tt'],
['rrr', 9, 'tt'],
['b', 9, 'a'],
[1,123,8]
];
let getDuplicates =(arr,i)=>{
let temp = {};
return [...new Set(arr.map(v=>v[i]).filter(v=>(v in temp ? !0 : !(temp[v]=1))))];
}
console.log(getDuplicates(a,1))