Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
how do I return the whole object without seeing [object Object]?

This code updates the object properties and their values but when I try to return the object, I get [object Object] on my screen. How do I Return the whole object?

<div id="demo"></div>

//the javascript code
const recordCollection = {
        2548: {
            albumTitle: 'Slippery When Wet',
            artist: 'Bon Jovi',
            tracks: ['Let It Rock', 'You ive Love a Bad Name']
        },
        2468: {
            albumTitle: '1999',
            artist: 'Prince',
            tracks: ['1999', 'Little Red Corvette']
        },
        1245: {
            artist: 'Robert Palmer',
            tracks:[]
        },
        5439: {
            albumTitle: 'ABBA Gold'
        }
    }

    function updateRecords(records, id, prop, value) {
        let demo = document.getElementById('demo');
        
        if (prop !== "tracks" && value !== "") {
            records[id][prop] = value;
        } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
            // let add = [];
            records[id][prop] == [value];
        } else if (prop === "tracks" && value !== "") {
            records[id][prop].push(value);
        } else if (value === "") {
            delete records[id][prop];
        }

        demo.innerHTML = records;
        return demo;
    }

    updateRecords(recordCollection, 5439, 'artist', 'ABBA');
<!-- The html code -->
<div id="demo"></div>

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You have to create a string at some point. JSON.stringify(records) is the closest you will get to printing the object.

//the javascript code
const recordCollection = {
        2548: {
            albumTitle: 'Slippery When Wet',
            artist: 'Bon Jovi',
            tracks: ['Let It Rock', 'You ive Love a Bad Name']
        },
        2468: {
            albumTitle: '1999',
            artist: 'Prince',
            tracks: ['1999', 'Little Red Corvette']
        },
        1245: {
            artist: 'Robert Palmer',
            tracks:[]
        },
        5439: {
            albumTitle: 'ABBA Gold'
        }
    }

    function updateRecords(records, id, prop, value) {
        let demo = document.getElementById('demo');
        
        if (prop !== "tracks" && value !== "") {
            records[id][prop] = value;
        } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
            // let add = [];
            records[id][prop] == [value];
        } else if (prop === "tracks" && value !== "") {
            records[id][prop].push(value);
        } else if (value === "") {
            delete records[id][prop];
        }

        demo.innerHTML = JSON.stringify(records, null, 2);
        return demo;
    }

    updateRecords(recordCollection, 5439, 'artist', 'ABBA');
<!-- The html code -->
<pre id="demo"></pre>

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!