Array of objects with some objects containing another array, need to filter it by values within the object containing an array

Dennis Jansen

Sorry for the long title question didn't know how to properly formulate my question.

So this is the question, I got an array which stores objects, and within those objects there's another array with the key tags and which is an array that gets filled with tags.

Object:

var arr = new Array;

arr[0] = {
            "like": 0,
            "dislike": 0,
            "answer": "body",
            "tags" : ["offline", "facebook"],
            "comments" : []
        };

arr[1] = {
            "like": 0,
            "dislike": 0,
            "answer": "body",
            "tags" : ["school", "online"],
            "comments" : []
        };

arr[2] = {
        "like": 0,
        "dislike": 0,
        "answer": "body",
        "tags" : ["school", "offline"],
        "comments" : []
    };

The [0], [1] indexes respresent and ID, but that isn't really important here.

The problem is when I try to filter it with(I know it only tries to filter arr[0] atm but I will write a loop so it goes through all the indexes):

var toFind = "offline";

var filtered = arr[0].filter(function(el) {
  return el.tags === toFind;
});

console.log(filtered);

It says arr[0].filter is not a function, but got no idea why it thinks arr[0] should be taken into account as a function.

When I remove the [0] it gives back a blank array...

So my question is how can I get the .filter function to give back the complete object based on tags in the tags array. So that if I search for the tag "school" it gives back arr[1] and arr[2], the complete objects.

Hope I my question is crystal clear and that I provided enough information. If you guys need anymore information, feel free to ask me for it. Or if you want me to clarify things, just ask :)

Hope you guys can help me!

dgeare

Like has been said, arr[0] points to an object, which doesn't have a filter method. You need to call filter on the array itself

var tagToFind = "school";
var arr = new Array;

arr[0] = {
            "like": 0,
            "dislike": 0,
            "answer": "body",
            "tags" : ["offline", "facebook"],
            "comments" : []
        };

arr[1] = {
            "like": 0,
            "dislike": 0,
            "answer": "body",
            "tags" : ["school", "online"],
            "comments" : []
        };

arr[2] = {
        "like": 0,
        "dislike": 0,
        "answer": "body",
        "tags" : ["school", "offline"],
        "comments" : []
    };

var filtered = arr.filter(function(item){
  return item.tags && //to account for undefined tags properties
    item.tags.includes(tagToFind); //if you're supporting IE you will need to look and check each element instead
});

console.log(filtered);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Array of objects from array containing objects with values contained in another array

Filter an array of objects based on array property containing all values from another array

Filter an object containing a key that contains an array of objects using an array of strings

How to convert object containing Objects into array of objects

How to convert objects containing array into object of objects

How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

filter struct objects from object element containing array of string

array containing objects to single object containing said objects

remove object property containing array inside array of objects if element in another array of objects

Merge objects containing array

destructing array containing objects

Filter an array of objects by a property containing a substring in AngularJS

Filter an Array containing several arrays with objects inside

How to fetch values from object containing array objects?

Flatten an array of objects containing key\values

mongodb - query for array of objects containing two values

Typescript literal object containing an array of objects

Convert an object containing more objects into an array

Class Object containing an array of other objects

Filter array of objects based on values in another array

Array of objects filter by another array values

Array of objects to array of objects containing arrays

Need to return objects containing the key which referred from array of objects

Filter an array within an array of objects

filtering an array of objects based on another array containing keys in javascript

Map array containing another array with objects and display them in a table in javascript

Filter object containing array of object

Array of objects to a single object containing all said objects Dataweave

Delete an array containing Objects MongoDB