Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I try to grab the object from the array, the type is undefined. Therefore I cannot use a method from the undefined object as it doesn't exist. I am relatively new to JavaScript and I have come straight from Java so the way of retrieving objects is kind of new to me. This is what I currently have.

var fleetAmount = 0;
var fleets = [];
function Fleet(number) {
    this.number = number;
    this.activities = [];
    this.addActivity = function (activity) {
        this.activities.push(activity);
    };
    fleets.push(this);
}
var getFleet = function(fleetNumber) {
    return fleets[fleetAmount - fleetNumber];
}

This is where I try to grab the object and preform the function

const Fl = require(‘fleet.js’);
const fleet = Fl.getFleet(fleetNumber);
fleet.addActivity(activity);

I am also working in Node.js, which is how I am using the require method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
129 views
Welcome To Ask or Share your Answers For Others

1 Answer

In combination with the answer from @audzzy I changed the getFleet() function so that it would be more efficient. I tested it out and it worked. This is what I used

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...