-3

Here I am setting the empty object with key (publisher) and value author;

Also I tried and I push them in the empty array

let publisherAutors = {};
let pushedAutorsPerPublisher = [];

With this for loop i`m taking the elements from my library , which are books(objects), and i take out the publishers and the autors

for (let i = 0; i < library.length; i++) {
  const element = library[i];
  autorCount = 0;

I think my problem is here , with the if statements, i wanna compare but something is going wrong

  if (
    element.publisher === element.publisher &&
    element.autor === element.autor
  ) {
    console.log(`Yes`);

Here i push to the empty array

    pushedAutorsPerPublisher.push(element.publisher, element.autor);
    console.log(element.publisher, element.autor);
  }

Here I`m adding key/ value to the empty object, but Its not helping me , because they goes only once in the empty object

  publisherAutors[element.publisher] = element.autor;
}

console.log(publisherAutors);
console.log(pushedAutorsPerPublisher);

My main problem is to make object , or array that holds them and count how many different authors have each publisher .

big small
  • 11
  • 2
  • Run through your array, and build a separate object that uses publishers as keys, with "how many authors" as value, and just count everything up. Then "finding the publisher with the most/fewest authors" becomes a matter of analyzing _that_ object instead of your original array. Or, of course, generate your array with the author count already in there. – Mike 'Pomax' Kamermans Jan 31 '22 at 18:03
  • Thanks a lot for the ideas Mike !, I`m very fresh with the programming and next time I will follow your advice Barmar . – big small Jan 31 '22 at 18:43

1 Answers1

2

My suggestion would be to store every publisher's published authors into their respective arrays, and then use filter and indexOf to filter out the duplicates. Then, return the largest array.

Here is a link to how you would use filter and indexOf to filter duplicates.

skoleosho97
  • 173
  • 1
  • 2
  • 13
  • No worries, do you mind accepting my answer as correct if it ends up working out? Good luck with your project! :) – skoleosho97 Jan 31 '22 at 19:02