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

149
Vistas
Join two object arrays and display remaining values in 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"
}];

I'm trying to join using position key with two other object arrays(pdf_header and pdf_footer) and save the remaining object key values in pdf_header_output and pdf_footer_output respectively. If the value is not present in obj, then an empty object should push.

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

Expected Output:

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

I tried this, but didn't work as expected

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

0

You need to map over each item in pdf_header or pdf_footer and find the matching element in obj. Then extract all properties except the position one.

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

If you can't use modern JS here is my solution:

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