0

I have this:

let points = something
let obj1 = {
        points: points
}

The points value changes

let obj2 = {
        points: points
}

I want that obj1.points is equal to the first points value and obj2.points the second. Here's more of the code:

constructor($){

    let stats = $(".player-stats-info").get(0);
    let points = parseInt(stats.children[5]) || 0;
    this.rush = {
        points: points
    }

    stats = $(".player-stats-info").get(1);
    this.hikaBrain = {
        points: points
    }

    stats = $(".player-stats-info").get(2);
    this.skyWars = {
        points: points
    }

    stats = $(".player-stats-info").get(3);
    this.octogone = {
        points: points
    }

    //etc
}
6
  • 3
    If you create obj2 after the value of points changes, you'd get that behaviour.
    – VLAZ
    Commented Apr 19, 2020 at 22:35
  • you can use lodash to clone the variable.
    – parthjani7
    Commented Apr 19, 2020 at 22:37
  • 1
    I see you reassigning the stats variable, but not the points variable, so I'm not sure what you're looking for - the points always stays the same given the code there, right? Commented Apr 19, 2020 at 23:39
  • That is because in my objects, points is not the only entry depending on the stats variable
    – Wxcc
    Commented Apr 19, 2020 at 23:44
  • You only assign to points once, though - it's only depending on the initial stats variable. Do you mean that you need to do points = parseInt(stats.children[5]) || 0; each time? (can you just do that, then? or is the question that you want to write less repetitive code?) Commented Apr 19, 2020 at 23:52

1 Answer 1

0

It sounds like you want a function that returns an object with the right structure:

const makeObj = stats => ({
  points: parseInt(Object.byString(stats.children[5], d)) || 0,
  gameCount: parseInt(Object.byString(stats.children[7], d)) || 0,
  victoryCount: parseInt(Object.byString(stats.children[9], d)) || 0,
  defeatCount: parseInt(Object.byString(stats.children[11], d)) || 0,
  gameTime: ms(Object.byString(stats.children[13], d)) || 0,
  killCount: parseInt(Object.byString(stats.children[15], d)) || 0,
  deathCount: parseInt(Object.byString(stats.children[17], d)) || 0,
});

Then just call makeObj with the stats:

this.rush = makeObj($(".player-stats-info").get(0));
this.skyWars = makeObj($(".player-stats-info").get(1));
// etc
11
  • But I want it reusable, I want an obj3, obj4 etc.
    – Wxcc
    Commented Apr 19, 2020 at 22:46
  • Are you really reassigning points in between each object declaration? Can you post your full code so we can have an idea of what exactly you're doing? Commented Apr 19, 2020 at 22:49
  • 1
    Please post the relevant code in the question, so as to make it on-topic for the site Commented Apr 19, 2020 at 22:52
  • 1
    @Hunam Can you post the relevant code in the question, to make it on-topic for Stack Overflow? Commented Apr 19, 2020 at 23:24
  • 1
    @Hunam You have to actually post the code into the question itself. Ok, I'll do it for you Commented Apr 19, 2020 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.