Apex
Apex

Reputation: 227

How do I access an 'Array' inside an Array using Angular?

I have an array I have created inside of my component which combines a name and then another array with the number of times the name has been selected.

I'm struggling to convert the second array (within the first array) into a number or say something like Array.length.

Here is the logged out Array:

0: (2) ["Elliott Lester", Array(4)]
1: (2) ["Frank Miller", Array(3)]
2: (2) ["Adam McKay", Array(3)]
3: (2) ["Zola", Array(3)]
4: (2) ["Saul Metzstein", Array(3)]
5: (2) ["Baltasar Kormákur", Array(2)]
6: (2) ["J.A. Bayona", Array(2)]
7: (2) ["Katsuhiro Ôtomo", Array(1)]
8: (2) ["Darren Aronofsky", Array(1)]
9: (2) ["Alex Proyas", Array(1)]
10: (2) ["Andy Humphries", Array(1)]
11: (2) ["Ken Loach", Array(1)]
12: (2) ["Francis Ford Coppola", Array(1)]
13: (2) ["Andrew Stanton", Array(1)]
14: (2) ["Danny Boyle", Array(1)]
length: 15
__proto__: Array(0)

//component code

  let data = groupBy(this.mom,'name')
  this.data = Object.keys(data);
  this.values = Object.keys(data).map(key => data[key]);
  const voting = {};
  this.data.forEach((key, idx) => voting[key] = this.values[idx])
  let votesresults = [];
  for (var player in voting) {
    votesresults.push([player, voting[player]]);
  }
  votesresults.sort(function(a,b){
    return  b[1].length - a[1.].length;
  });
  this.values = votesresults;
  console.log(votesresults);

I'd like to display the array(s) as:

Elliott Lester, 4 - but can't seem to figure out how. Any hekp would be greatly appreciated.

I've tried binding length to the array which is called values so:

{{ values[i].length }} - this just displays the length of the entire array. 
{{ values[i].Array.length }} - knew this wouldn't work, but gave it a try anyway, because I was out of ideas! 

Upvotes: 1

Views: 10928

Answers (1)

holydragon
holydragon

Reputation: 6728

From what I understand, this should be your array object, and this is how you can get the desired result from it.

const array = [
  [
    "Elliott Lester",
    [
      {id: 71, status: 1, sort: null, number: 9, name: "Elliott Lester"},
      {id: 65, status: 1, sort: null, number: 10, name: "Elliott Lester"},
      {id: 66, status: 1, sort: null, number: 10, name: "Elliott Lester"},
      {id: 83, status: 1, sort: null, number: 9, name: "Elliott Lester"}
    ]
  ],
  [
    "Frank Miller",
    [
      {id: 71, status: 1, sort: null, number: 9, name: "Frank Miller"},
      {id: 65, status: 1, sort: null, number: 10, name: "Frank Miller"},
      {id: 66, status: 1, sort: null, number: 10, name: "Frank Miller"}
    ]
  ],
  [
    "Adam McKay",
    [
      {id: 71, status: 1, sort: null, number: 9, name: "Adam McKay"},
      {id: 65, status: 1, sort: null, number: 10, name: "Adam McKay"},
      {id: 66, status: 1, sort: null, number: 10, name: "Adam McKay"}
    ]
  ]
];
array.forEach(function(data){
  console.log(data[0], data[1].length);
});

Upvotes: 2

Related Questions