0

I have this data

var data = [
  {
    title: "App development summary",
    category: [],
  },
  {
    title: "to experiment 2",
    category: [],
  },
  {
    title: "Some of these books I have read",
    category: [
      {
        _id: "5f7c99faab20d14196f2062e",
        name: "books",
      },
      {
        _id: "5f7c99faab20d14196f2062f",
        name: "to read",
      },
    ],
  },
  {
    title: "Quora users and snippets",
    category: [
      {
        _id: "5f7c99feab20d14196f20631",
        name: "quora",
      },
    ],
  },
  {
    title: "Politics to research",
    category: [
      {
        _id: "5f7c9a02ab20d14196f20633",
        name: "politics",
      },
    ],
  },
];

Say I want to get all the entries with the category of books. I tried doing this:

var bookCat = data.map(note => {
 return note.category.map(cat => {
   if(cat.name === "books" ) return note
 })
}) 

But the result comes back with some empty and undefined arrays.

I was able to filter the empty arrays (at one point) but not the undefined

enter image description here

Edit

In plain English "if the object ha category.name "books", give me the title"

4
  • Do you want the name: "to read", object to be included? Commented Oct 11, 2020 at 21:54
  • @CertainPerformance yes, that's part of the categories. I'm trying to get the book that has the category of 'book' the fact that it also has the category 'to read' doesn't matter. Commented Oct 11, 2020 at 21:57
  • Please show an example of the results you expect to receive Commented Oct 11, 2020 at 22:28
  • @Phil the result from the above would be data[2] that's the only object with the category of book. I misread the answer given. the code actually works but doesn't loop. If there was another object like data[2] the code provided in the first answer would not show it. this jsbin.com/popibedugu/edit?js,console selects the right one but leaves out the other Commented Oct 11, 2020 at 22:57

1 Answer 1

2

Filter by whether the array's category contains at least one name which is books:

var data=[{title:"App development summary",category:[]},{title:"to experiment 2",category:[]},{title:"Some of these books I have read",category:[{_id:"5f7c99faab20d14196f2062e",name:"books"},{_id:"5f7c99faab20d14196f2062f",name:"to read"}]},{title:"Quora users and snippets",category:[{_id:"5f7c99feab20d14196f20631",name:"quora"}]},{title:"Politics to research",category:[{_id:"5f7c9a02ab20d14196f20633",name:"politics"}]}];

const output = data
  .filter(item => item.category.some(
    ({ name }) => name === 'books'
  ));
console.log(output);

If there's guaranteed to be only a single matching object, use .find instead:

var data=[{title:"App development summary",category:[]},{title:"to experiment 2",category:[]},{title:"Some of these books I have read",category:[{_id:"5f7c99faab20d14196f2062e",name:"books"},{_id:"5f7c99faab20d14196f2062f",name:"to read"}]},{title:"Quora users and snippets",category:[{_id:"5f7c99feab20d14196f20631",name:"quora"}]},{title:"Politics to research",category:[{_id:"5f7c9a02ab20d14196f20633",name:"politics"}]}];

const output = data
  .find(item => item.category.some(
    ({ name }) => name === 'books'
  ));
console.log(output);

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

4 Comments

That's not what I want. I want the entire parent of the category
In plain English "if the object ha category.name "books", give me the title". That's what I'm having trouble with, getting the parent array the moment I've filtered the category name.
Sorry again, what if there are two objects with the category 'books'. jsbin.com/popibedugu/edit?js,console I added another entry and doesn't show
@relidon It looks to work? It shows a result array of length 2 - both objects with a books get included

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.