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

223
Views
Cómo combinar dos matrices diferentes con diferentes claves en una sola matriz en jquery

Tengo dos matrices array1 y array 2.Array1 tiene valores con un nombre de clave "discount_price" y array2 tiene valores con un nombre de clave "regular_price". por ejemplo: matriz_precio_descuento , matriz_precio_regular

Cuando los fusiono usando la combinación de matrices, obtengo una sola matriz con valores combinados. es decir, si matriz1 tiene 10 elementos y matriz2 tiene 10 elementos, se fusiona en una matriz con 20 elementos. matriz_combinada .

lo que quiero es en cambio una matriz con, por ejemplo:

 array{"discount_price":10,"regular_price":2}, array{"discount_price":4,"regular_price":3},

Cómo puedo conseguir esto ?

 $(values).each(function(key, value) { //console.log(value); if( value.discount_price !==undefined){ discount_price_array.push({ discount_price: value.discount_price }) } }); $(values).each(function(key, value) { //console.log(value); if( value.regular_price !==undefined){ regular_price_array.push({ regular_price: value.regular_price }) } }); var finalarray =$.merge(discount_price_array,regular_price_array )
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usando map y 'desestructurando (...)'

map : permite iterar sobre una matriz y devolver la nueva matriz de la misma longitud, donde usted decide qué se devolverá en cada iteración.

(...) spread , le permite difundir las propiedades (propiedades externas) en un nuevo objeto o matriz, básicamente está asignando todas las propiedades del objeto al nuevo objeto, ya que está extendiendo dos objetos en el mismo objeto nuevo, están siendo fusionado

 // if both of them have the same length let arr1 = [{'property1': 10 }, {'property1': 10 },]; let arr2 = [{'property2': 20 }, {'property2': 20 },]; let result = arr1.map((object, index)=> ({...object, ...arr2[index] })); console.log(result);

map only simple

 // if you want to pick which property to assign let arr1 = [{'property1': 10 }, {'property1': 10 }]; let arr2 = [{'property2': 20 }, {'property2': 20 }]; let result = arr1.map((object, index)=> { object['property2'] = arr2[index]['property2']; return object; }); 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!