Using the .map method to filter through JSON data

I'm facing some challenges filtering a .json file to exclude private videos on an app related to a YouTube channel. There are certain videos marked as private that I do not want to display within the app. Can someone assist me in properly filtering the .json data?

Currently, when utilizing the code snippet below to filter the data, it returns an empty json object. However, if I remove the .map function, it displays the complete list of videos including the private ones.

getPlayListVideos(listId: string) {
    return this.http.get('https://www.googleapis.com/youtube/v3/playlistItems?key=' + this.apiKey + '&fields=items/snippet/resourceId/videoId,items/snippet/publishedAt,items/snippet/title,items/snippet/thumbnails/high/url&playlistId=' + listId +'&part=snippet,id&maxResults=25')
    .map((res) => {
      return res.json()['items'].filter(item => {
        if(item.snippet.title === 'Private video'){
          return false;
        }
       });
    })
}

Answer №1

To avoid filtering the current item, you should return true. If you don't return anything in that case, it will be treated as false or undefined implicitly. Here is how you can achieve this:

if(item.snippet.title === 'Private video'){
    return false;
} else {
    return true;
}

Alternatively, you can simplify it to

return item.snippet.title !== 'Private video'
.

For better formatting, I prefer writing it like this:

  getPlayListVideos(listId: string): any[] {
       return this.http.get('https://www.googleapis.com/youtube/v3/playlistItems?key=' + this.apiKey + '&fields=items/snippet/resourceId/videoId,items/snippet/publishedAt,items/snippet/title,items/snippet/thumbnails/high/url&playlistId=' + listId + '&part=snippet,id&maxResults=25')
              .map(res => res.json()['Items'])
              .map((items: any[]) => items.filter(item => item.snippet.title !== 'Private video'))
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular Swiper Integration: Maintaining Scope Inside Configuration Callback

Trying to grasp the concept of working with callbacks to maintain scope can be a bit tricky, as I've been experiencing. Specifically, I've been struggling with a callback in the Swiper library for Angular, where I am unable to keep my desired sco ...

Custom AngularJS menu directive using a JSON file to generate submenus

As a newcomer to angularJs, I am looking to create a dynamic menu with submenus using a .json file. It's important for me to be able to easily add new menus and submenus through the same .json document. <a href="#" ng-repeat="item in menuHeader"&g ...

Ways to resolve encoded titles in Youtube API search results

I am currently utilizing the youtube-search 1.1.4 package to search for videos. However, I am encountering an issue where the titles of the results are encoded with &amp; or &#39; instead of simply & and ' and so forth. For instance, an e ...

Using jQuery to input a value that returns the [object Object]

While working with jQuery in asp.net, I encountered an issue where the value assigned to a hidden field (hfstockcode) is returning [object Object]. The console output shows v.fn.v.init[1]. How can I retrieve the correct value for the hidden field? $(docum ...

Unable to locate "angular", attempting to retrieve and validate values from the HTML form

I'm relatively new to Angular and I'm looking to utilize angular.module('FooApp', []); in order to fetch HTML form data and conduct validation within my .ts component file. Unfortunately, it appears that "angular" isn't being reco ...

retrieve the source code from all .js links found within the document

There is a file labeled urls.txt. https://website.tld/static/main_01.js https://website.tld/static/main_02.js https://website.tld/static/main_03.js .... All the source code from every .js file in allsource.txt needs to be extracted. Instructions: To ge ...

Utilizing the Ternary Operator for conditional rendering in React is causing issues with HTML elements

Why does React display the "signin" div when the user is not true, but displays the children of the "user-profile" div inside the "signin" div when the user is true? I have tried multiple solutions to fix this issue, but it persists. If anyone knows how t ...

Guide on updating and storing changes in a JSON file named 'file.json' using jQuery or JavaScript

I'm working with a JSON file and attempting to make updates using jQuery. However, I'm encountering an issue where I can't seem to save the update once the script has finished running. Is there a way to save the update without relying on se ...

What is the reason behind the prevalence of using export class xxx in angular4 projects instead of export default class xxx?

Understanding the distinction between export class xxx and export default class xxx is crucial. However, I've noticed that numerous angular4 projects utilize export class xxx in this manner: @Component({ selector: 'call-record', templ ...

Swap out the traditional for loop with a LINQ query utilizing the any method

In my TypeScript code, I have the following snippet: public executeTest(test: Test): void { const testFilters: Record<string> = getTestFilters(); let isTestingRequired: boolean = false; for (let i: number = 0; i < testFilters.leng ...

Invoke a function within the React library through ReactDOM.render

Within my index.js file, I have the following code: const store = createStore( combineReducers({ i18n: i18nReducer }), applyMiddleware(thunk) ); syncTranslationWithStore(store) store.dispatch(loadTranslations(translationsObject)); stor ...

Grouping Columns in an HTML Table using Angular 4

I'm currently faced with the task of retrieving flat data from an API and presenting it in an HTML table using Angular 4. I'm a bit unsure about how to iterate over the data, possibly using a for-each loop. I have attempted to utilize ngFor but I ...

Tic-Tac-Toe: The square's value stays unchangeable

Currently, I am working on creating a tic-tac-toe game using pure vanilla Javascript, so I am aiming to keep it as simple as possible. I have run into an issue and need some guidance. The main requirement is that once a square has been clicked and filled ...

Obtain form data as an object in Vue when passed in as a slot

Currently, I am working on developing a wizard tool that allows users to create their own wizards in the following format: <wiz> <form> <page> <label /> <input /> </page> <page> <label /> ...

Struggling to get JQuery timing events to function properly?

Encountering timing issues with the events in my self-made simon memory game. Experimented with setTimeout but struggling to figure out which events to time and the appropriate duration for them to be distinguishable. $('#play').click(function( ...

Is it necessary to bump the major version if I make updates to a react module that does not affect existing code functionality, but may cause Jest snapshot tests to break?

Imagine I am developing a module for a react component and currently working on a PR to introduce a new feature. Along with this new feature, I have also made changes to the component by refactoring it to eliminate certain internal parts that were previou ...

When two-dimensional arrays meet destructuring, it results in a type error

Can anyone clarify TypeScript behavior in this scenario? JavaScript const test1 = [ ['a', 'b'], ['c', 'd'], ]; const test2 = [ ...[ ['a', 'b'], ['c', ' ...

What is the reason for my JSON reference returning as undefined?

Here's my JavaScript code: $.getJSON("/aTest.json", function (jsonObj) { $("#testJSONBtn").click(function () { var val = ""; for (var i = 0; i <= jsonObj.events.length; ++i) { val += jsonObj.events[i].title + ", " + ...

Exploring the Overlapping of Objects in Three.js

I am dealing with multiple meshes in my code and I am trying to figure out how to make the renderer display the object that should be in front, somewhat similar to what is shown in this example: --> . I have read about render depth, but I am unsure of h ...

Tips for editing bootstrap-vue table columns using non-Latin characters?

I need to create a table using the Cyrillic alphabet, but the keys of the object must be in the Latin alphabet within the program. Example : export default { data() { return { wmsFields: ['№', 'Наименование', ...