1

I have an array of object called shows:

const shows =  [{
   "name": "John Doe Show",
   "rating": 1234,
   "alternative_name": null,
},
{
   "name": "Jane Doe Show",
   "rating": 4321,
   "alternative_name": "Alternate Name 2",
}
{
   "name": "Other Show",
   "rating": 8842,
   "alternative_name": "Alternate Name 3",
}];

I specifically want to get the alternative_name in a function getAlternateName(). But I want the condition for it to be that it should not be null. How would I go about it?

Edit: For example, for the Jane Doe Show, it has an alternative_name with "Alternate Name 2," while for the John Doe Show, alternative_name is null. When I console.log(getAlternateName()), it would show:

[ { alternative_name: null },
{ alternative_name: 'Alternate Name 2' },
{ alternative_name: 'Alternate Name 3' } ]

However, I just want it to show "Alternate Name 2 and Alternate Name 3, without showing null. How would I go about it?

7
  • can you write more detail about your question, shows is an array now. Commented Nov 12, 2019 at 7:09
  • Filtered based on which property? And what result that you expect when the "alternative_name" is null? Commented Nov 12, 2019 at 7:12
  • For example, for the Jane Doe Show, it has an alternative_name with "Alternate Name 2," while for the John Doe Show, alternative_name is null. When I console.log(getAlternateName()), it would show both alternative_name, however, I just want it to show the alternative_name for the Jane Doe Show Commented Nov 12, 2019 at 7:16
  • then, you expect to filter them by "alternative_name" only? so, data without "alternative_name" will not be shown? Commented Nov 12, 2019 at 7:22
  • Yes, I only want data for "alternative_name." I don't want null to be shown with the other data when I console.log the function. Commented Nov 12, 2019 at 7:26

3 Answers 3

2

Extract the alternative_name with map, then filter for nun-null values. I took the liberty to just return an array of strings, not objects, to keep the result short & focused.

const shows =  [{
   "name": "John Doe Show",
   "rating": 1234,
   "alternative_name": null,
},
{
   "name": "Jane Doe Show",
   "rating": 4321,
   "alternative_name": "Alternate Name 2",
},
{
   "name": "Other Show",
   "rating": 8842,
   "alternative_name": "Alternate Name 3",
}];

const alternativeNames = shows
    .map(({alternative_name}) => alternative_name)
    .filter(x => x)
    
console.log(alternativeNames)

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

2 Comments

waldo233, this is the best solution for your problem ;)
This is better than my solution, use it instead!
0

EDIT: There is a better solution by AndreasPizsa.

Here is a function that will return an array of all the alternative names in the array shows. If there is no alternative name in the array, it will not appear in the returned array.

function getAlternateName() {
    this.alternateNames = [];
    for (var i = 0; i < shows.length; i++) {
        if (shows[i].alternative_name) {
            this.alternateNames.push(shows[i].alternative_name);
        }
    }
    return this.alternateNames;
}

const shows = [{
    "name": "John Doe Show",
    "rating": 1234,
    "alternative_name": null,
  },
  {
    "name": "Jane Doe Show",
    "rating": 4321,
    "alternative_name": "Alternate Name 2",
  },
  {
    "name": "Bill Doe Show",
    "rating": 1243,
    "alternative_name": "Alternate Name 3",
  },
  {
    "name": "Jill Doe Show",
    "rating": 4312,
    "alternative_name": null,
  }
];

function getAlternateName() {
  this.alternateNames = [];
  for (var i = 0; i < shows.length; i++) {
    if (shows[i].alternative_name) {
      this.alternateNames.push(shows[i].alternative_name);
    }
  }
  return this.alternateNames;
}

console.log(getAlternateName());

Comments

0

You can use two methods:

const shows =  [{
   "name": "John Doe Show",
   "rating": 1234,
   "alternative_name": null,
},
{
   "name": "Jane Doe Show",
   "rating": 4321,
   "alternative_name": "Alternate Name 2",
},
{
   "name": "Other Show",
   "rating": 8842,
   "alternative_name": "Alternate Name 3",
}];

function getAltName(oName) {
  const result = shows.filter(x => x.name === oName)[0];
  return result.alternative_name || "Can not find your expected data.";
}

// this method to trims and take only data with alternative_name.
function trimByAltName() {
  return shows.filter(x => x.alternative_name !== null);
}

console.log('Get alternative name only:', getAltName('Jane Doe Show'));

console.log('Get Filtered Data:', trimByAltName());

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.