3

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);
}
  • Does this answer your question? [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – VLAZ Oct 12 '20 at 18:52
  • Please show the `getFleet` function. – Lioness100 Oct 12 '20 at 19:18
  • Please take a look at the question you posted yesterday, as I regarded this issue when you posted your answer: https://stackoverflow.com/questions/64309315/using-function-of-an-object-after-grabbing-it-from-array/64309691?noredirect=1#comment113719317_64309691 – audzzy Oct 12 '20 at 22:43

1 Answers1

2

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)
}
Lioness100
  • 8,260
  • 6
  • 18
  • 49