1

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! 
3
  • It's not really clear what your initial structure is. Would be really nice if you provide MCVE or a JSON which you would like to parse, or at least the code which logs the array. Commented Dec 21, 2018 at 8:42
  • this.array.forEach(e => { e.arrayInside.forEach(c=>{ //some logic }) }) Commented Dec 21, 2018 at 8:42
  • I'll add it up there now. Thank you. Commented Dec 21, 2018 at 8:42

1 Answer 1

2

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);
});

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

6 Comments

This really helps thank you. I have this displaying as a loop in the view where there are more than one name. How do you then display for say two or three names that have different lengths?
Please update your question so I can see your object clearly. I now have no idea of what your object is going to look like.
I have added in the full log into the original question.
I have edited the answer using .forEach(). Hope it is what you are looking for. Also, please accept it as an answer if it does solve your issue.
brilliant, thank you for the help, this is exactly what I needed. Many thanks. :)
|

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.