0

We have variable currentCategoryId and array of object

[
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
]

for example const currentCategoryId = 12 and need to return new filter array

[
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
]

or

const currentCategoryId = 16

return

[
    { id: 65, title: "hi there!", categories: [16] },
]

My code

 const postsByCategory = posts.filter(({ categories }) =>
    categories.filter(
      categoryId => categoryId === 
       currentCategoryId
    )
)

returned

[
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
]

3 Answers3

1

This should be simple to implement with the Array.prototype.filter() and the Array.prototype.includes() function.

const arr = [
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
];

const currentCategoryId = 12

const result = arr.filter((arr) => {
    return arr.categories.includes(currentCategoryId);
});

console.log(result);

The filter() method creates a new array with all elements that pass the test implemented by the provided callback function. The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. In this example, it checks if the categoryId is included in the categories field of the current array element.

Aykut Yilmaz
  • 183
  • 2
  • 6
0

Instead of using .filter in the inner array for categories , you should use the .includes method to check if the categoryId exists . A simple and generic solution using filter and includes array methods , pass array and the categoryId . It returns the filtered data

var array  =[
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
]

function filterData(data,categoryId){
  return data.filter(obj => obj.categories.includes(categoryId));
}

console.log(filterData(array,12))
console.log(filterData(array,16))
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
0

Solution #1: Using the the Array helpers (prototypes) .filter() and .includes() is the simplest and easiest way to return an array of all elements in the data array that include and category number you are looking for:

var data = [
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
];
console.log('searching for items with category 12...');
console.log(data.filter(a => a.categories.includes(12)));
console.log('searching for items with category 16...');
console.log(data.filter(a => a.categories.includes(16)));

Solution #2: Don't be this one-liner guy/gal even though it works by returning an array of name value pairs from the data array (using Object.entries()) and then reducing that to an array of values from the data array that only contain the category number you want (using Array.prototype.indexOf()).

var data = [
    { id: 18, title: "hello world!", categories: [12, 18] },
    { id: 12, title: "hi hi hi", categories: [12] },
    { id: 65, title: "hi there!", categories: [16] },
];

// Nasty one-liner
const fcbid = (arr, id) => Object.entries(arr).reduce((a,c) => ((c[1].categories.indexOf(id) != -1) && a.push(c[1]), a), []);

console.log('searching 12...');
console.log(fcbid(data, 12));
console.log('searching 16...');
console.log(fcbid(data, 16));
apena
  • 2,091
  • 12
  • 19