How to enter objects properly?

1

What happens is that I'm creating an object calling clanes , within which are all the clans, in an array clan , and another with its supposed owners , but as I can know , that, in the case of my example, the clan 'DW' , belongs to 'Javier' ?

var clanes = {
  name: [],
  owner: []
  
};


function crear(clan, owner) {
  
  var c = clan.toString();
  var d = owner;
  
  clanes.name.push(clan);
  clanes.owner.push(d);
  
}

crear("DW", "Javier")
crear("ZT", "Pedro")
console.info(clanes);

It occurred to me something like going through all the properties of the object, and since they are all arrays, if they have the same index, they have found the owner, but I did not clarify ..

I tried this:

var dueño = "";

for(var i in clanes) {
  if(clanes.name[i] == clanes.owner[i]) {

    dueño = clanes.owner[i];
    console.info("El dueño de :" + clanes.name[i] + " es: " + dueño);

  }

}
    
asked by Eduardo Sebastian 28.06.2017 в 23:15
source

1 answer

3

Try this way, I think it's more appropriate for what you need. Here two separate arrangements are not being used to store the information. I'm just using an array where each item within that array has the object with the necessary information for each clan:

var clanes = [];

function crear(clan, owner) {
  clanes.push({name:clan,owner:owner});
}

crear("DW", "Javier");
crear("ZT", "Pedro");

console.info(clanes);

for(var i in clanes) {      
    console.info("El dueño de :" + clanes[i].name + " es: " + clanes[i].owner);
}
    
answered by 28.06.2017 / 23:35
source