I need assistance with updating the console log after assigning a new value to a sub object property in JavaScript. Can anyone

Lately, I've been encountering an issue with assigning values to a sub-object in JavaScript. Here is my sample code:

var user = {
        name: {
            fname: 'Apple'
        }
    };

console.log(user);
user.name.fname = 'Orange';
console.log(user);

Even though the console shows the output twice, the value of fname always displays as Orange. However, I want the output to be Apple and then Orange. How can I achieve this, or what is actually happening here? Please explain what is going on.

Answer №1

Give this a shot:

let person = {
        fullName: {
            first: 'Apple'
        }
    };

console.log(person.fullName.first);
person.fullName.first = 'Orange';
console.log(person.fullName.first);

Here's to your success!

Answer №2

To print an object, you can utilize console.dir(object)

console.dir(person); 

Avoid using console.log(user.age);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a memory storage problem when trying to include additional images to create a word document using the docx node

const imageResponse = await axios.get(url[0], { responseType: "arraybuffer", }); const buffer = Buffer.from(imageResponse.data, "utf-8"); const image = Media.addImage(doc, buffer); Within a loop that runs 1 ...

What is the best way to transform an Iterator[JsValue] into a JsArray?

Is there a way to merge multiple JsValue objects from an Iterator into a single JsValue in Play 2.1? ...

The issue of undefined database columns arises when attempting to transmit data from an HTML form to a MySQL database via Express

My primary objective is to develop a RestAPI using Node.js and test it in a small HTML application. With the guidance of my instructor, I successfully created the RestAPI based on an example and customized it to work with my own MySQL database. Testing ea ...

Looking for assistance with populating an ASP.net gridview by utilizing a JSON datasource in c# programming

I need to create a gridview that automatically populates with JSON data. The JSON folder is set up and functioning correctly for saving data. However, I am struggling to populate the ASP.net gridview with my JSON file. I attempted to use Visual Studio&a ...

Utilizing React JS and Recoil to manipulate select/selectFamily for a targeted attribute

I implement Recoil state management in ReactJS to store keyboard letter data, for instance lettersAtom = atom( key: 'Letters' default: { allowed : ['A','C','D'] pressedCounter : {'A':2, ' ...

Tips for creating a jQuery AJAX function in JavaScript that is triggered by an onclick event

Can anyone help with creating a generic JavaScript function to handle AJAX requests and calling it using 'onclick'? I want to ensure that the loaded Ajax results still work with this function. My setup involves JQuery and PHP, so something like t ...

tips for integrating JavaScript into JSX

Currently, I am in the process of learning Javascript and React and have encountered a problem that I need assistance with. I am trying to implement a JavaScript function in my JSX files but cannot figure out where to place it. Here is what I am attempti ...

Ways to customize weekend colors in v-calendar (distinct colors for Saturdays and Sundays)

Looking to customize the appearance of weekends in your calendar by changing their colors? I have a component for the v-calendar that can help with that. Here's the code: <div id='app'> <v-date-picker title-position="left&quo ...

What is the best way to prevent event propagation in d3 with TypeScript?

When working with JavaScript, I often use the following code to prevent event propagation when dragging something. var drag = d3.behavior.drag() .origin(function(d) { return d; }) .on('dragstart', function(e) { d3.event.sourceEvent ...

I aim to display a well-optimized mobile view by utilizing Bootstrap CSS

I need my website to be mobile responsive and I want the mobile view to have specific changes. Simply adding cd-xs-12 or even cd-sm-12 is not achieving the desired outcome. https://i.sstatic.net/0GU2h.jpg This is the current code I have: ...

Scrolling infinitely within the side menu using ion-infinite-scroll

I'm encountering an issue with using Ion-infinite-scroll within ion-side-menu, as the load more function is not being triggered. Is it permitted to utilize ion-infinite-scroll inside ion-side-menu? Although I have added the directive, the method "lo ...

Issues encountered with Material UI components in a React application

After installing Material UI, I encountered an issue while trying to import it into my React project. The error message displayed was " Module not found: Can't resolve 'material-ui/core/Button " Is there a way to properly import it from node_mod ...

Inability to assign a value to an @input within an Angular project

I recently started using Angular and I'm currently trying to declare an input. Specifically, I need the input to be a number rather than a string in an object within an array. However, I'm encountering difficulties and I can't figure out wha ...

Storing data in a database using SQLAlchemy by converting a JSON list into database records

Currently, I have a list with JSON data structured as follows: print(type(listed)) # <class 'list'> print (listed) [ { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bf3cbece6eae2e7a5e8e4e ...

invoking a function from two separate pages

I have created a React web app that is supposed to hide the navigation bar on the login page and then display it again after a successful login on all other pages. I achieved this by creating a function in App.js, but the issue arises when trying to call t ...

Issue encountered when making jQuery AJAX requests

Every time I make this AJAX request var BASE_URL = "http://127.0.0.1:8000/index.php"; $.get(BASE_URL + "?r=category") .done(function (data) { alert('success'); }) .fail(function (error) { ...

Tips on effectively deserializing JSON elements with identical element collections in C#

I am facing a challenge with deserializing a collection of elements in JSON that contains nested collections of the same elements in C#. How can I achieve this? So far, I have attempted to solve this by creating a C# class for the elements and defining pr ...

Can someone please explain the distinction between $http.get() and axios.get() when used in vue.js?

I'm feeling a little bit puzzled trying to differentiate between $http.get() and axios.get(). I've searched through various sources but haven't found any answers that fully satisfy me. Can someone please offer some assistance? ...

An unanticipated JSON token encountered while parsing the DataTable

Here is the json string that I am working with: { "Orders": [{ "SubOrderNo": "0582715", "ItemNo": "20415541", "ItemType": "ART", "ItemName": "Fish", "TemplateName": "TP1234", "ObjectType": "MPP", ...

Is there a way to easily uncheck all MaterialUI Toggle components with the click of a button or triggering an outside

If you have a set of <Toggle /> components in an app to filter clothes by size, and you want to reset all the filters with a single click on a button rather than unchecking each toggle individually. Is it possible to achieve this using materials-ui ...