Using reduce to create new Object

So I’m just recreating the examples in this video

I’m trying to recreate from 5:30

My attempt

const albums = [
  { id: '1', artist: "Kanye"    , name: "College Dropout", tracks: 21},
  { id: '2', artist: "D'angelo", name: "Voodoo"			, tracks: 13},
  { id: '3', artist: "Kendrick", name: "DAMN."			, tracks: 14},
];

var lookup = albums.reduce((acc, album) => {
  return {...acc, [album.id]: album} // Something wrong with this line?
	}, {});

I’m not getting the expecting result (object with ids as keys and previous objects as values).

executing the code in the console gives what you are wanting for

image

What result are you seeing? how does it diverge from what you want?

1 Like

Ahhh so it works, thanks a ton. I was using the es6console and it didn’t seem to return anything. Any idea why this is?

you are not doing anything with the value lookup

if it’s a console you can add lookup on its own line after it is defined and it should print like I did in the browser console, or you can use console.log to print it for sure

My bad I should’ve added the console.log prior screenshooting

Nothing is logged out? Also that cross at line 8?

Nothing is logged out?

Nothing for me either. If I delete the code and do a simple log statement and hit run, that works. But anything more complicated and it doesn’t work.

Also that cross at line 8?

The squiggly line means that it doesn’t recognize that code, specifically the spread operator. If select the “stage-1” toggle on the left, it goes away. That seems odd to me because it is part of ES6, and has been around for a long time.

What is this site supposed to be? You can’t find a tool on the side of the road and assume what it does. It looks to me like it was meant to see how different versions of React compile and transpile. It also seems out of date - it goes up to es2017 but we’re on es2021 and es2022 is right around the corner. I’d just forget about this site.

2 Likes

Noted. Thanks a ton. I just started using Visual Code studio and need just a bit of help getting started.

I tried executing the code I’ve posted and I get this "No debugger available, can not send variables’ messag’. What does this mean exactly? And how do I go about running my code.

I would suggest you check out the quokkajs extension. It is very handy for testing and playing around with JS in VS Code.

1 Like

Thanks, I went ahead and installed Quokka. Although I still want to know exactly what this can no sent ‘variables’ message means exactly and how can I simply use console.log(lookup) to return the expected object mentioned previously?

As in how can I have my code executed in the terminal as shown in the video? Still very new to this so apologies for what probably seems like obvious incompetence.

But it is logging out lookup, right?

As in I have the variable lookup as the argument to the console.log() method? Yes?

Do I need to install some sort of debugger extension for node.js?

Do I need to install some sort of debugger extension for node.js?

If you want to run a debugger, then yes. I don’t know, I almost never use the VSC terminal. If I do, I just use the terminal, but I almost always just use a separate terminal window. I see that you have “Debug Console” selected. Is there a reason you are using that instead of the terminal? I’m sincerely asking - I haven’t used that much.

When I run the Run and Debug feature, I get the same message as you, when I try to expand the output. After a little research, it sounds like you need to set up a launch.json and set up a debugger.

1 Like

The Debug Console is the only place where anything seems to be happening for me lol

I assume I’m meant to be seeing something in the “output” tab?

When I try to create a launch.json folder I get this regardless of what I try to name the file.

It feels like everytime I try to solve one problem two more problems pop up.

It may not be valid because you are trying to give to a folder a name that should be of a file

For me, the terminal tab was working as expected.

1 Like

You can just run it using node.js from the console node someFileName.js.

If you want to use the debugger you should be able to click the “Run and Debug” button and then pick node.js from the dropdown.

If you want to customize it you can click the link that says “create a launch.json file”, select node.js from the dropdown and it should create a .vscode folder with a launch.json file inside it. Make sure you opened VS Code inside the folder with the file you want to run and that you select the file in the file explorer before switching to the debug tab in VS Code. Now you should be able to click the run icon or press F5.

Follow the docs in the link that was posted or look on YouTube I’m sure there are setup instruction videos.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.