Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
Une dos matrices de objetos y muestra los valores restantes en JavaScript
var obj = [{ "position": "Bottom Left", text: "2" }, { "position": "Top Center", image: logo, width: 22 }, { "position": "Bottom Right", text: "1" }, { "position": "Top Left", text: "9" }];

Estoy tratando de unirme usando la clave de position con otras dos matrices de objetos ( pdf_header y pdf_footer ) y guardar los valores clave de objeto restantes en pdf_header_output y pdf_footer_output respectivamente. Si el valor no está presente en obj , entonces debería empujarse un objeto vacío.

 var pdf_header = [{ "position": "Top Left" }, { "position": "Top Center" }, { "position": "Top Right" }]; var pdf_footer = [{ "position": "Bottom Left" }, { "position": "Bottom Center" }, { "position": "Bottom Right" }];

Rendimiento esperado:

 var pdf_header_output = [{ text: "9" }, { image: logo, width: 22 }, {}]; var pdf_footer_output = [{ text: "2" }, {}, { text: "1" }];

Intenté esto , pero no funcionó como se esperaba

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Debe mapear cada elemento en pdf_header o pdf_footer y encontrar el elemento coincidente en obj . Luego extraiga todas las propiedades excepto la position uno.

 var obj = [{ "position": "Bottom Left", text: "2" }, { "position": "Top Center", image: 'logo', width: 22 }, { "position": "Bottom Right", text: "1" }, { "position": "Top Left", text: "9" }]; var pdf_header = [{ "position": "Top Left" }, { "position": "Top Center" }, { "position": "Top Right" }]; var pdf_footer = [{ "position": "Bottom Left" }, { "position": "Bottom Center" }, { "position": "Bottom Right" }]; function findByPosition(object, positions) { return positions.map(({ position }) => { const item = object.find(({ position: objPosition }) => objPosition === position); if (item) { const { position: throwaway, ...rest } = item; return rest; } return {}; }); } var pdf_header_output = findByPosition(obj, pdf_header); var pdf_footer_output = findByPosition(obj, pdf_footer); console.log(pdf_header_output); console.log(pdf_footer_output);
 I'm var pdf_header_output = [{ text: "9" }, { image: logo, width: 22 }, {}]; var pdf_footer_output = [{ text: "2" }, {}, { text: "1" }];

about 4 years ago · Juan Pablo Isaza Denunciar

0

 let obj = [ { 'position': 'Bottom Left', text: '2' }, { 'position': 'Top Center', image: 'logo', width: 22 }, { 'position': 'Bottom Right', text: '1' }, { 'position': 'Top Left', text: '9' } ] let pdf_header = [ { 'position': 'Top Left' }, { 'position': 'Top Center' }, { 'position': 'Top Right' } ] let pdf_footer = [ { 'position': 'Bottom Left' }, { 'position': 'Bottom Center' }, { 'position': 'Bottom Right' } ] let pdf_header_output = pdf_header.map(header => { const items = obj.filter(item => item.position === header.position)[0] return items !== undefined && 'position' in items ? ( ({ position, ...o }) => o )(items) : {} } ) console.log(pdf_header_output) let pdf_footer_output = pdf_footer.map(header => { const items = obj.filter(item => item.position === header.position)[0] return items !== undefined && 'position' in items ? ( ({ position, ...o }) => o )(items) : {} } ) console.log(pdf_footer_output)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Si no puede usar JS moderno, aquí está mi solución:

 var obj = [{ "position": "Bottom Left", text: "2" }, { "position": "Top Center", image: 'logo', width: 22 }, { "position": "Bottom Right", text: "1" }, { "position": "Top Left", text: "9" }]; var pdf_header = [{ "position": "Top Left" }, { "position": "Top Center" }, { "position": "Top Right" }]; var pdf_footer = [{ "position": "Bottom Left" }, { "position": "Bottom Center" }, { "position": "Bottom Right" }]; var pdf_header_output = getPropsFrom(pdf_header); var pdf_footer_output = getPropsFrom(pdf_footer); function getPropsFrom(array) { var props = []; for (var i = 0; i < array.length; i++) { props.push(getPropByPosition(array[i])); } return props; } function getPropByPosition(element) { for (var i = 0; i < obj.length; i++) { if (obj[i].position === element.position) { return getCopyWithoutPosition(obj[i]); } } return {}; } function getCopyWithoutPosition(o) { var copy = Object.assign({}, o); delete copy.position; return copy; } console.log(pdf_header_output); console.log(pdf_footer_output);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda