Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:01:31+00:00 2026-06-03T12:01:31+00:00

If I have an object: var array = []; var theobject = null; array.push({song:The

  • 0

If I have an object:

var array = [];
var theobject = null;

array.push({song:"The Song", artist:"The Artist"}, {song:"Another Song", artist:"Another Artist"});

and I do:

for(var i = 0; i < array.length; i++)
if(array[i].song == "The Song") {
theobject = array[i];
break;
}

If I then change theobject by doing:

theobject.song = "Changed Name";

I am having problems where despite myself trying to set ONLY “theobject.song” to be equal to “Changed Name”, array[0].song becomes set to “Changed Name” also.

What I want is “theobject.song” to become “Changed Name” while array[0].song remains “The Song”.

What is the best way to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:01:33+00:00Added an answer on June 3, 2026 at 12:01 pm

    You will never get a reference to your object in the loop. Try:

    for(var i = 0; i < array.length; i++)
     if(array[i].song === "The Song") {
     theobject = array[i];
     break;
    }
    

    That will give a reference to the object, and you will be able to change the objects song property.

    If you want to use a copy of the object, then you’ll have to do a manual copy. E.g.

    function clone(obj) {
      var copy = {};
      for (var attr in obj) {
       if (obj.hasOwnProperty(attr)) {
         copy[attr] = obj[attr];
       }
      }
      return copy;
    }
    

    And your loop becomes:

    for(var i = 0; i < array.length; i++)
     if(array[i].song === "The Song") {
     theobject = clone(array[i]);
     break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following object: var l={a:1,b:2,c:5}; I want to get the length of
Let's say I have a JavaScript object: function a(){ var A = []; this.length
I have an object which is assigned a number of properties: var project_array:Array =
I have the object var dataformdata={key1:value1,key2:value2}; then I add some more values with the
I have a javascript object with two array's as shown, var Object = {'name':
I have array of objects: var aoo = [{},{},{},....{},{},{}]; I need a optimized function
I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo',
Say I have an array of Employee Objects: var Employee = function(fname, age) {
I have a map array with objects stuffed with variables looking like this: var
I have a object var foo = { jon : {age: 'old', feeling: 'sad'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.