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

159
Views
Comparar matriz de objetos con matriz en JavaScript

Entonces, me encontré con este problema mientras trabajaba en un proyecto para un cliente. Básicamente, tengo una matriz con objetos y una matriz de valores, y necesito llenar la matriz con objetos, con objetos para cada elemento de la matriz. Es un poco confuso decirlo, pero aquí está el código y cuál debería ser el resultado:

 // The Array, and the array with objects var worksheet = [{"student_name": "Test 1", "file": "some_file_name.txt"}, {"student_name": "Test 3", "file": "file": "some_file_name.txt"}] var data = {"student_names": ["Test 1", "Test 2", "Test 3", "Test 4"]}

La idea aquí es recorrer data.student_names y agregar a cada estudiante que no está en la hoja de trabajo,

 // Output Should Be: var worksheet = [ {"student_name": "Test 1", "file": "some_file_name.txt"}, {"student_name": "Test 3", "file": "some_file_name.txt"}, {"student_name": "Test 2", "file": ""}, // This wasn't here {"student_name": "Test 4", "file": ""}, // This wasn't here ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Concepto

Repita a través de data.student_names y verifique si el nombre existe en la hoja de trabajo. Si lo encuentra, coloque el objeto estudiante en foundArr ; de lo contrario, almacene el nombre del estudiante con un archivo vacío en notFoundArr . Por último, concatene ambas matrices para obtener el resultado.

Código

 const worksheet = [{"student_name": "Test 1", "file": "some_file_name.txt"}, {"student_name": "Test 3", "file": "some_file_name.txt"}]; const data = {"student_names": ["Test 1", "Test 2", "Test 3", "Test 4"]}; let foundArr = []; let notFoundArr = []; data.student_names.forEach(s => { let studentFound = worksheet.find(ws => ws.student_name === s); if (studentFound !== undefined){ foundArr.push(studentFound); } else { notFoundArr.push({"student_name": s, "file":""}); } }); let result = foundArr.concat(notFoundArr); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

 const worksheet = [ { 'student_name': 'Test 1', 'file': 'some_file_name.txt' }, { 'student_name': 'Test 3', 'file': 'some_file_name.txt' } ] const data = { 'student_names': [ 'Test 1', 'Test 2', 'Test 3', 'Test 4' ] } const result = data['student_names'] .map(value => { let record = worksheet.filter(item => item['student_name'] === value) let file = record.length === 0 ? '' : record[0].file return { 'student_name': value, 'file': file } } ) console.log(result)

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!