Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Working with two dimensional arrays in JavaScript to detect repeated values

I have been working on an example that detects repetitions by letter-number with the following JavaScript code.

But it's not working. The idea is that it detects that the combination A-1 is repeated; using a control matrix.

This is the items JSON structure:

{
letter: 'A',
number: '1',
},
{
letter: 'B',
number: '2',
},
{
letter: 'A',
number: '1',
},
{
letter: 'C',
number: '2',
},

And this is the code I am trying to:

var isRepeated = false;
var numItems = items.length;
if (numItems > 0) {
    var item; 
    var letter;
    var number;
    var matrix = [[]];
    for (var j = 0; j < numItems; j++) {
        item = items[j];
        letter = item.letter;
        number = item.number;
        if (matrix[letter][number] == undefined) {
            matrix[letter][number] = true;
        } else {
            isRepeated = true;
            break;
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

matrix[letter][number]

You're trying to access elements of an array using string keys but arrays use integer indexes

Here's an alternative if you just want to know if something is repeated

const props = [{
    letter: 'A',
    number: '1',
  },
  {
    letter: 'B',
    number: '2',
  },
  {
    letter: 'A',
    number: '1',
  },
  {
    letter: 'C',
    number: '2',
  },
]

const didRepeat = (array) => array.some(((hash) => ({letter, number}) => {
  const key = `${letter}_${number}`;

  if (hash.has(key)) return true;
  return hash.add(key), false;
})(new Set()))

console.log(didRepeat(props))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!