hey guys so i'm a beginning of javascript and I have a question... I have 3 functions let's say:
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}
}
let's say I want to make use of these return values in another function, so..
const p = () => {
const arr = [returned value from function a, returned value from function b, returned value from function c]
return arr
}
how would I somehow use the objects that functions a, b, c in order to return an array of objects from function p?
thank you!
Following what you have asked:
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' }
Therefore if you made the p function:
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' }
// ]
It would be far more likely that p would take some kind of arguments, then would use them inside. For example:
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());