1

I've an array with players in it. Every player has its name (array[i].name) and power (array[i].power). Now I want to add dynamically grades for every player. Every grade should have its own categories with achieved points.

Data structure example:

array[i].football.shooting=5;

array[i].football.dribbling=3;

I tried to push the name of the grade first to each player and then add the categories as an extra array. But not even .push() worked as I mentioned:

for (var i = 0; i < array.length; i++) {
        array[i].push(gradeName);
}

Can please someone give me a hint how to solve this?

Thank you very much!

5
  • so array contains player objects? Commented Dec 2, 2015 at 8:08
  • Try this: array[i].grade = gradeName; Commented Dec 2, 2015 at 8:08
  • Try `array[i]["grade"] = gradeName. Push appends value to array. Commented Dec 2, 2015 at 8:09
  • You have to call push to the array of categories, not to the array of players. How do you keep the array of categories in the player object? Commented Dec 2, 2015 at 8:09
  • Should'nt it be array[i].football[gradeName] = gradeValue; ?? Commented Dec 2, 2015 at 8:09

4 Answers 4

1

Probably something like this:

JSFiddle

var array = [{
    name: 'hello',
    power: 1
}, {
    name: 'world',
    power: 3
}];

var getGrades = function () {
    return {
        shooting: 5,
        dribling: 3
    };
}

for (var i = 0; i < array.length; i++) {
    array[i].football = [];
    array[i].football.push(getGrades());
}

console.log(array);

You should post also your desired output to get a specific answer.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you! but how can I also dynamically set the names of the categories like 'shooting' and 'dribbling' to dynamic inputs? As I understood with this solution I've to have static categories, don't I?
I don't know where you get that data, I simply put a static object for example. You could do a function to get the gradeName.
If I get gradeName and two categories (shooting and fighting) into the function. How can I do that to not hardcode the categories names but take it from the function?
I updated the answer with a simple example of using a function you then need to add some conditions or what you need to get the skill scores.
Ok, but I'm still not sure how to name the categorie names shooting and dribbling after some user input for example.
|
1

In JavaScript I'd suggest working with objects within array - I know thats what you are already doing but look at a architecture like that:

var players = [
   {
      name: "Playername1",
      attributes: [
         {name: "football", skill_level: { dribbling: 1, passes: 3}},
         {name: "basketball", skill_level: {range_shot: 3, passes:1}}
      ]
   }
]

With that architecture you can push an entire new player object to the array. You can also loop through the attributes, check for existing attributes, add missing ones and edit current ones -> great variety.

if you'd like to add an attribute to that architecture it would be like:

var newSkill = { name: "flying plane", skill_level: { flight: 4, landing:0 }};

for(var i = 0; i < players.length; i++){
     players[i].attributes.push(newSkill);
}

Hope this could help a bit.

UPDATE

Let's say you have received a user input through a form and save that to a variable userInputName

Then you can simlpy add a dynamically named attribute like that :

var newSkill = { name: userInputName, skill_level: { flight: 4, landing:0 }};

for(var i = 0; i < players.length; i++){
     players[i].attributes.push(newSkill);
}

4 Comments

thank you! but how can I also dynamically set the names of the categories 'flight' and 'landing' to dynamic inputs?
if you want to change the attributes name in future you must change the attributes content - let me uptdate my answer for you. Answer edited
Ok, but can I pass a variable to be a category name? The categories flight and landing are hardcoded. How can I name the categories after some user input for example?
Sure you can use any variable in that schema without any problem - I updated the answer for you with an extra example for that.
0

This is an associative array not your regular array

try

for (var i = 0; i < array.length; i++) {
        array[i][ gradeName ] = {};
}

Comments

0

According to your data structure example adding should be something like this:

var grade = 'footbal';
var category = 'shooting';
var points = 5;
array[i][grade] = array[i][grade] || {};
array[i][grade][category] = points;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.