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

117
Views
¿Cómo obtener solo el valor no nulo de la propiedad del objeto de la matriz de objetos?

En la matriz de objetos recibida del servidor, quiero procesar y recuperar solo el valor no nulo de la propiedad en el objeto. ¿Cómo puedo cambiarlo en mi función?

 const arr = [{ text01 : 'name', text02 : 'email@gmail.com', text03 : '010-1234-5678', text04 : 'adress1', text05 : 'adress2', text06 : null, text07 : null, text08 : null, }, { text01 : 'name1', text02 : 'email2@gmail.com', text03 : '010-1255-5148', text04 : 'adress3', text05 : 'adress4', text06 : null, text07 : null, text08 : null, }] getDataArr(arr) { arr.forEach(item => { const aaa = []; for (let key in item) { if (item[key] !== null) { const value = item[key]; aaa.push({ key, value }); } } console.log(aaa); });

Obtener el valor como

 const arr = [{ text01 : 'name', text02 : 'email@gmail.com', text03 : '010-1234-5678', text04 : 'adress1', text05 : 'adress2' }, { text01 : 'name1', text02 : 'email2@gmail.com', text03 : '010-1255-5148', text04 : 'adress3', text05 : 'adress4', }]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Una forma concisa de eliminar valores nulos es filtrar las entradas de objetos, creando un objeto a partir de los restantes. Con eso, simplemente asigne sobre la matriz de entrada...

 function nonNullValues(obj) { return Object.fromEntries( Object.entries(obj).filter(([key, value]) => value !== null) ) } const result = theArray().map(nonNullValues); console.log(result) function theArray() { return [{ text01: 'name', text02: 'email@gmail.com', text03: '010-1234-5678', text04: 'adress1', text05: 'adress2', text06: null, text07: null, text08: null, }, { text01: 'name1', text02: 'email2@gmail.com', text03: '010-1255-5148', text04: 'adress3', text05: 'adress4', text06: null, text07: null, text08: null, } ] }

about 4 years ago · Juan Pablo Isaza Report

0

Lodash si no te importa. Usando: ominBy y isNull

 const arr = [{ text01 : 'name', text02 : 'email@gmail.com', text03 : '010-1234-5678', text04 : 'adress1', text05 : 'adress2', text06 : null, text07 : null, text08 : null} , { text01 : 'name1', text02 : 'email2@gmail.com', text03 : '010-1255-5148', text04 : 'adress3', text05 : 'adress4', text06 : null, text07 : null, text08 : null} ] const result = arr.map(obj => _.omitBy(obj, _.isNull)); console.log(result);
 .as-console-wrapper{min-height: 100%!important; top: 0}
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

 const arr = [ { text01: "name", text02: "email@gmail.com", text03: "010-1234-5678", text04: "adress1", text05: "adress2", text06: null, text07: null, text08: null, }, { text01: "name1", text02: "email2@gmail.com", text03: "010-1255-5148", text04: "adress3", text05: "adress4", text06: null, text07: null, text08: null, }, ]; const nonNull = [] for(const item of arr) { const obj = {} for(const key in item) { if(item[key]) obj[key] = item[key] } nonNull.push(obj) } console.log(nonNull)

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!