hola chicos, soy un principiante de javascript y tengo una pregunta ... tengo 3 funciones, digamos:
const a = (x, y, z) => { return {x, y, z} } const b = (a, b, c) => { return {a, b, c} } const c = (d, e, f) => { return {d, e, f} }digamos que quiero hacer uso de estos valores devueltos en otra función, entonces...
const p = () => { const arr = [returned value from function a, returned value from function b, returned value from function c] return arr }¿Cómo podría usar de alguna manera los objetos que funcionan a, b, c para devolver una matriz de objetos de la función p?
¡gracias!
Siguiendo lo que has pedido:
const result1 = a(1, 'dog', null) // result1 would be { x: 1, y: 'dog', z: null } const result2 = b('cat', 2, null) // result1 would be { a: 'cat', b: 2, c: null } const result3 = c(3, 4, 'pig') // result3 would be { d: 3, e: 4, f: 'pig' }Por lo tanto, si hiciste la función p:
const p = () => { return [ a(1, 'dog', null), b('cat', 2, null), c(3, 4, 'pig') ] } const result1 = p() // result1 would be: // [ // { x: 1, y: 'dog', z: null }, // { a: 'cat', b: 2, c: null }, // { d: 3, e: 4, f: 'pig' } // ]Sería mucho más probable que p tomara algún tipo de argumentos y luego los usara internamente. Por ejemplo:
const p = (arg1, arg2, arg3) => { return [ a(arg1, arg2, arg3), b(arg1, arg2, arg3), c(arg1, arg2, arg3) ] } const result2 = p(1, 'dog' null) // result2 would be: // [ // { x: 1, y: 'dog', z: null }, // { a: 1, b: 'dog', c: null }, // { d: 1, e: 'dog', f: null } // ] const one = (a, b, c) => { return {a, b, c}; } const two = (a, b, c) => { return {a, b, c}; } const three = (a, b, c) => { return {a, b, c}; } const result = () => { let firstObj = one(10,20,30); let secondObj = two(40,50,60); let thirdObj = three(70,80,90); // Returns all of the objects in an array. return [firstObj, secondObj, thirdObj]; } console.log(result());