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
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" }]; 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)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);