Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop objects from sharing values

To set this up I create 2 fleet objects, store them in an array, then print the array to console. This is what I get after both fleet objects are created.

[
  { number: 1, activities: [], addActivity: [Function (anonymous)] },
  { number: 2, activities: [], addActivity: [Function (anonymous)] } 
]

The addActivity function of the fleet object adds an activity to the activities array of the specified object. This is the function.

    fleet.addActivity = function(activity) {
        this.activities.push(activity);
    }

However, whenever I use this function, it changes both fleet objects in the array so that they have the same number as the one that I specified. For example,

const fleet = Fleet.getFleet(1);
fleet.addActivity('Run');

This is printed in console:

[
  {
    number: 1,
    activities: [ 'Run' ],
    addActivity: [Function (anonymous)]
  },
  { number: 1, activities: [], addActivity: [Function (anonymous)] }
]

If I were to do it again using

const fleet = Fleet.getFleet(2);
fleet.addActivity('Walk');

Console would print:

[
  {
    number: 2,
    activities: [ 'Walk', 'Run' ],
    addActivity: [Function (anonymous)]
  },
  { number: 2, activities: [], addActivity: [Function (anonymous)] }
]

Does anyone have any idea of how to fix this?

EDIT- Here is the getFleet function:

function getFleet(fleetNumber) {
    let result = exports.fleets.filter(function (e) {
        return e.number = fleetNumber;
    });
    return result[0];
}

I also tried the lodash deep cloning method and I still have the same issue. This is what my addFleet function currently looks. My previous method of creating fleets has been commented out.

function addFleet(number) {
    /*let fleet = {
        number: number,
        activities: []
    };
    fleet.addActivity = function(activity) {
        this.activities.push(activity);
    }*/
    const deepClone = lodash.cloneDeep(originalFleet);
    deepClone.number = number;
    exports.fleets.push(deepClone);
    exports.fleetAmount += 1;
    exports.currentFleets.push(number);
}
like image 971
Isaiah Easton Avatar asked Feb 24 '26 16:02

Isaiah Easton


1 Answers

Your issue is inside your getFleet() function. Instead of checking each object number for equality, you're actually overwriting the variable. The fix is as simple as changing one equal sign to three.

function getFleet(fleetNumber) {
    let result = exports.fleets.filter(function (e) {
        return e.number === fleetNumber; // check for equality instead of overwriting
    });
    return result[0];
}

Also, you're overcomplicating the function. Since you only need to find the first match, you should use Array.prototype.find()

function getFleet(fleetNumber) {
  return exports.fleets.find((e) => e.number === fleetNumber)
}
like image 192
Lioness100 Avatar answered Feb 26 '26 04:02

Lioness100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!