-1

How would I filter out the following Titles using .filter? I'm expecting the output to be something like: {"Capuchin Monkey", "Capybara"} I'm working with JSON that looks like this:

{
  "d": {
    "results": [
      {
        "__metadata": {
          "id": "N/A",
          "type": "N/A"
        },
        "Courses": {
          "results": [
            {
              "__metadata": {
                "id": "N/A",
                "type": "N/A"
              },
              "Title": "Capuchin Monkey"
            },
            {
              "__metadata": {
                "id": "N/A",
                "type": "N/A"
              },
              "Title": "Capybara"
            },

JS snippet:

// Courses/Title is what I'm interested in
    axios.get([redacted] + "/getByTitle('Categories')/items?$select=Title,Description,Courses/Title,SortOrder&$expand=Courses&$orderby=Title&$top=1000",
                {
                    method: "GET",
                    credentials: "include",
                    mode: "no-cors",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    }
                }),
   // irrelevant code
        ]).then(axios.spread((cat, lib, admn) => {
            _categories = cat.data.d.results; // -------- //

            this.loadCategories();
        })).catch(error => {
            console.log(error);
        });

getCategories(){ 
        return _categories;
    }

loadCategories(){ 
        let categs = _categories,
            trainingCrs = _categories.d.results.filter(x => {
                return {
                    "crsTitle": x.Courses.results.Title // code smell
                }
            });
Bodrov
  • 840
  • 1
  • 15
  • 29
  • "filter out" - what is your expected result? An array with all titles? The original object with all titles removed? Please add the expected output to your question! – Constantin Groß Apr 26 '19 at 15:53
  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – demo Apr 26 '19 at 15:53

3 Answers3

1

I think you need is map, not filter. Something like this:

var json = { "results": [
  {
    "__metadata": {
      "id": "N/A",
      "type": "N/A"
    },
    "Courses": {
      "results": [
        {
          "__metadata": {
            "id": "N/A",
            "type": "N/A"
          },
          "Title": "Capuchin Monkey"
        },
        {
          "__metadata": {
            "id": "N/A",
            "type": "N/A"
          },
          "Title": "Capybara"
        }]}}]};
const reducedResult = json.results.reduce((act, val)=> act.concat(val));
const titles = reducedResult.Courses.results.map((value)=>value.Title);
console.log(titles);
Juan S.Montoya
  • 306
  • 2
  • 9
0

To get a list of Titles such as {"Capuchin Monkey", "Capybara"}, you better use Array​.prototype​.map() rather than Array​.prototype​.filter() .

var json = {
  "d": {
    "results": [
      {
        "__metadata": {
          "id": "N/A",
          "type": "N/A"
        },
        "Courses": {
          "results": [
            {
              "__metadata": {
                "id": "N/A",
                "type" : "N/A"
              },
              "Title": "Capuchin Monkey"
            },
            {
              "__metadata": {
                "id": "N/A",
                "type": "N/A"
              },
              "Title": "Capybara"
            }
          ]
        }
      }
    ]
  }
}

// Use of .map
trainingCrs = json.d.results[0].Courses.results.map(x => x.Title);
console.log("Training title list: ", trainingCrs);

// Use of .filter
trainingCrs = json.d.results[0].Courses.results.filter(x => x.Title === "Capybara");
console.log("Training list filter on one Title", trainingCrs);
el-teedee
  • 1,293
  • 1
  • 15
  • 27
  • Hey, for some reason I'm getting an error in the console: `"Unable to get property 'results' of undefined or null reference"` – Bodrov Apr 26 '19 at 17:37
0
loadCategories(){ 
        let categs = _categories,
            trainingCrs = _categories.d.results.map((x) =>x.Courses.results.Title)
            });
  • Hi @Jitendra, when I try this I'm getting a `"TypeError: Unable to get property 'results' of undefined or null reference` in my console. – Bodrov Apr 26 '19 at 17:32