-3

Im trying to understand something seemingly incredibly simple.

console.log(${key}: ${value}) - works so long as return below is commented out.
Once, the 'return' statement is included below it, the function doesn't work and only returns the first key-val of the list.
It returns: a: something

Why is it that console.log(${key}: ${value}) and Return (${key}: ${value}) can be so different and how can I reformat to output the full key/value list from the function? My expected result is: "a: somestring, b: 42,c: false"

Thanks!

   const object1 = {
    a: "somestring",
    b: 42,
    c: false,
   };

   const defineKeyValPairs = (data) => {
      console.log(data); //outputs: {a: 'some string', b: 42, c: false}
      for (const [key, value] of Object.entries(data)) {
        console.log(`${key}: ${value}`); // returns "a: somestring, b: 42,c: false" so long as return below is commented out
        // return `${key}: ${value}`; // doesn't work - returns "a: somestring"
    }
   };

   defineKeyValPairs(object1);
Michael Martell
  • 141
  • 2
  • 16
  • 1
    `return` literally means **exit** or **go back**. `console.log` prints things into the console. Your question kind of sounds like: *Hair dryer dries hair, washing machine washes clothes - how can things be so different?* – Konrad Dec 05 '22 at 18:36
  • 2
    what do you think return is doing? – epascarello Dec 05 '22 at 18:38
  • I always thought, without any conditional logic, return outputs the value... in this case "[${key}: ${value}]" to an outer scope. Ie: provides the answer to the function – Michael Martell Dec 05 '22 at 18:41
  • 1
    It does return a value from the function, but you are only going to get 1 return from that function so it exits. It has nothing to do with logging. – epascarello Dec 05 '22 at 18:44
  • ...it explains the difference but doesn't answer the question. You'd think for taking a reputation hit, I'd at least be provided a solution... @Ivar – Michael Martell Dec 05 '22 at 18:44
  • "_but doesn't answer the question_" - From the answer to that question "_`return` - returns execution to the caller with optional result_". Doesn't that answer the question? If it returns execution to the caller, the function will no longer be executed. It only returns the first `return` it encounters. The loop wont continue. "_You'd think for taking a reputation hit, I'd at least be provided a solution_" - I don't really see that logic. Downvotes indicate that a post is not useful or doesn't show research effort. I'd argue that those are less likely to receive answers. – Ivar Dec 05 '22 at 18:49
  • 1
    So what you want it map() and return the result of the map. – epascarello Dec 05 '22 at 18:50
  • @ivar Ok, politely, I'll concede then that maybe I was not descriptive enough about what. I was trying to figure out. The question was how can I loop over the return statement then to achieve the desired result? – Michael Martell Dec 05 '22 at 18:52
  • @epascarello thank you... I'll try to figure that out. Thanks for the direction. – Michael Martell Dec 05 '22 at 18:53
  • 1
    @MichaelMartell That depends on what it is that you want to be the desired result. Should it return the string value `"a: somestring, b: 42,c: false"`? In that case create an empty string, loop over the values and concatenate to that string and then after the loop return the result. – Ivar Dec 05 '22 at 18:55
  • 1
    `return Object.entries(data).reduce(..., '')` or `return Object.entries(data).map(...).join(",")` – epascarello Dec 05 '22 at 18:59

1 Answers1

0

Thank you @Konrad, @epascarello and @Ivar for the direction.
Here's the solution:

const defineKeyValPairs = (data) => {
    console.log(data);
    return (Object.entries(data).map(([key, value]) => {
        return `${key}: ${value}`;
    }))
};

defineKeyValPairs(object1);

Returns: a: somestring, b: 42,c: false as the question requested. Hopefully this helps someone else out.

Michael Martell
  • 141
  • 2
  • 16