0

I have an array of object called actors:

const actors =  [{
   "name": "John Doe",
   "rating": 1234,
   "alternative_name": null,
   "objectID": "551486300"
},
{
   "name": "Jane Doe",
   "rating": 4321,
   "alternative_name": "Monica Anna Maria Bellucci",
   "objectID": "551486310"
}];

I specifically want to get the name and the ratings of the actors. I tried getting it by making a function called actorNameRating, but my code does not work.

const nameAndRating = function() {

    const actorName = nameAndRating.filter(name);
    return actorName;

    const actorRating = nameAndRating.filter(rating);
    return actorRating;
 };
2
  • 2
    You cannot return twice from a function. After first return the code below will not be executed. Commented Nov 12, 2019 at 5:42
  • What output do you want, exactly? Commented Nov 12, 2019 at 7:24

2 Answers 2

1

You can do by map a function and return only name and rating:

const actors =  [{
   "name": "John Doe",
   "rating": 1234,
   "alternative_name": null,
   "objectID": "551486300"
},
{
   "name": "Jane Doe",
   "rating": 4321,
   "alternative_name": "Monica Anna Maria Bellucci",
   "objectID": "551486310"
}];

const nameAndRating = function() {

   return actors.map(actor => ({
     name : actor.name,
     rating : actor.rating 
   }))
 };

console.log(nameAndRating())

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

Comments

1

any thing after the return will not be executed you can return both values as one string then separate it for example :

const nameAndRating = function() {
    const actorName = nameAndRating.filter(name);
    const actorRating = nameAndRating.filter(rating);
    return actorName+","+actorRating;
 };

or any other way you find better for you to use

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.