FAQ: Advanced Objects - Built-in Object Methods

This community-built FAQ covers the “Built-in Object Methods” exercise from the lesson “Advanced Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Built-in Object Methods

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Your code above may not be creating the newRobot expected by the SCT.

const newRobot = Object.assign({...}, robot);

We can use the object literal in the first positional argument, or as you have done, create a temporary object. However then we will have a stray object in memory. The literal in the argument is probably the better way to go.

3 Likes

Your code still uses the wrong variable for the new robot. If you wish to use a temporary variable, make it addFeatures, or something, not, newRobot since that is the variable the SCT is checking.

addFeatures = {laserBlaster: true, voiceRecognition: true,} 

newRobot = Object.assign(addFeatures, robot);

The stray object in this case is addFeatures since it never gets called again, just sits in memory taking up space. A very minor concern, here, but later on in more complex code it will be something to keep at a minimum. Intermediary variables are useful, though at times we don’t need them. This is one of those times.

3 Likes

shouldn’t make more sense to declare the laserBlaster and voiceRecognition properties before?

const laserBlaster = true;
const voiceRecognition = true;

const newRobot =
Object.assign({laserBlaster, voiceRecognition, robot});

console.log(newRobot);

That will not update the properties the way we expect…

{ laserBlaster: true,
  voiceRecognition: true,
  robot: 
   { model: 'SAL-1000',
     mobile: true,
     sentient: false,
     armor: 'Steel-plated',
     energyLevel: 75 } }

when it should look like,

{ laserBlaster: true,
  voiceRecognition: true,
  model: 'SAL-1000',
  mobile: true,
  sentient: false,
  armor: 'Steel-plated',
  energyLevel: 75 }

Right… ok… what I thought was that in practical sense, these properties maybe share by other objects, and so they should be declare by ‘let’ perhaps, but not for this example I understand.

1 Like

I was playing around with the last step of this exercise and found two viable solutions. I think the first is what the exercise is going for. Is the second also valid? They seem to produce the same output, but with the keys outputted in a different order.

// declares newRobot object and assigns values to two keys
const newRobot = {
  laserBlaster: true,
  voiceRecognition: true
};

// copies keys from robot object to newRobot object
Object.assign(newRobot, robot);

console.log(newRobot);
// declares newRobot object and copies keys from robot object to newRobot
const newRobot = Object.assign(robot);

// assigns values to two new keys in newRobot
newRobot.laserBlaster = true;
newRobot.voiceRecognition = true;

console.log(newRobot);

I am mostly curious if Object.assign(robot); is proper syntax. Obviously an error is produced when I write const newRobot = Object.assign(newRobot, robot); because newRobot cannot be used as an argument when it hasn’t been defined yet.

1 Like

Hi Cyber_bunnie

The first one is closer to what I think the task is asking for but see Roy’s comment above about creating a stray object.

The second one we’re creating the newRobot and then later adding new values to it, whereas I think the question is asking us to create them in one step.
Try creating it as you did here:

Object.assign(newRobot, robot);

but with the contents of the object in {} as the first argument rather than newRobot.

1 Like

Hi, I have the following solution for the 3rd point.

After this I was curious if I have done correctly so I tried it with the " solution" too.

Both seemed to be correct although with that (the solution) it adds a “,robot” at the end of the line as follows:

const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true}, robot);

Im not really sure why is that at the end as well as why would the latter be more correct that the first option on the picture?

const robot = {
	model: 'SAL-1000',
  mobile: true,
  sentient: false,
  armor: 'Steel-plated',
  energyLevel: 75
};

// What is missing in the following method call?
const robotKeys = Object.keys(robot);

console.log(robotKeys);

// Declare robotEntries below this line:
const robotEntries = Object.entries(robot);

console.log(robotEntries);

// Declare newRobot below this line:
const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true}, robot)
const addRobot = Object.assign(newRobot, robot);


//console.log(newRobot);
console.log(newRobot);


According to the following MDN link provided in this lesson, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Syntax, Object.assign(target, ...sources) will always only return the target value.

What exactly is the purpose of source then?

Also the instructions in the code say not to change the robot object. But in the (..., robot) aren’t we changing the object by adding to it?

const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true}, robot);
1 Like

Hi there.

I’d like to know what is the real-world use of the Object.assign() method? When would we want to use it?

We can clone an object and add new key-value pairs, and update existing key-values all in one stroke. This returns a new object and leaves the source object intact. There is good reason why this is built in to vanilla JS.

1 Like

Hey I was just wondering if my solution is okay? It passed.

const newRobot = Object.assign({}, {laserBlaster: true, voiceRecognition: true} );

This solution also passed:

const newRobot = Object.assign(this, {laserBlaster: true, voiceRecognition: true} );

Which is the better solution?

It’s not always easy to determine which is better, as that is more subjective than objective. In the case above, this is an empty object, though maybe not that obvious. The empty object in the first example is more explicit and less of a head turner to a lesser informed reader. Consider who one’s audience is and then decide if you want to explain, this or simply let it go with the literal and avoid questions.

1 Like

what does SCT mean? Ive searched and i cant find it

While experimenting with the example in the lesson I came to strange output see questions in the last comment:

const robot = {
	model: 'SAL-1000',
  mobile: true,
  sentient: false,
  armor: 'Steel-plated',
  energyLevel: 75
};

// Declare newRobot below this line:
const newProps = {
  laserBlaster: true, 
  voiceRecognition: true
  };

const newRobot = Object.assign(newProps, robot)

console.log (newProps); // prints object, because .assign() mutates target object and returns an object. That's fine.
console.log ('------------------------------');
console.log (Object.entries(newProps)); // prints array, because .entires() returns an array. That's fine.
console.log ('------------------------------');
console.log(`newRobot: ${newRobot}`); // Q1: why does it return an array? it's .assign(), it should return an object? Q2: why does it print "object Object" instead of the "key:value" or "key, value" like the other rows?

actually all the results are different if I try to concat a string to them… that’s a little strange to me.

The why of it is likely technical and worth a weekend of digging up the reading. It appears that template literals are not equipped to represent a data structure such as an object, so render the literal representation, [object Object]. We can wrestle the value to a serialized object with a JSON method, .stringify().

console.log(`newRobot: ${JSON.stringify(newProps)}`);
newRobot: {"laserBlaster":true,"voiceRecognition":true,"model":"SAL-1000","mobile":true,"sentient":false,"armor":"Steel-plated","energyLevel":75}

The difference is subtle. console.log() streams character data. ${} is a string literal, and not treated as character data. Note that JSON insists upon double quotes on all string values. Keys are actually strings.

1 Like

Understand this is an old post, can you explain why it would be good or bad to do this?

const newRobot = Object.assign({}, robot, {laserBlaster: true, voiceRecognition: true});

/*
{ model: 'SAL-1000',
  mobile: true,
  sentient: false,
  armor: 'Steel-plated',
  energyLevel: 75,
  laserBlaster: true,
  voiceRecognition: true }
*/

FYI, the course accepts the first return as correct. It also accepts:

{  robot: 
   { model: 'SAL-1000',
     mobile: true,
     sentient: false,
     armor: 'Steel-plated',
     energyLevel: 75 },
  laserBlaster: true,
  voiceRecognition: true, }