waldo233
waldo233

Reputation: 61

How do I return an array of objects that only contains a certain object in an array for Javascript?

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

Upvotes: 0

Views: 76

Answers (2)

Shubham Verma
Shubham Verma

Reputation: 5054

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())

Upvotes: 1

Amado
Amado

Reputation: 383

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

Upvotes: 1

Related Questions