简体   繁体   English

对象数组,某些对象包含另一个数组,需要通过包含数组的对象中的值对其进行过滤

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

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. [0],[1]索引表示和ID,但这在这里并不是很重要。

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): 问题是当我尝试使用它进行过滤时(我知道它仅尝试过滤arr [0] atm,但我会编写一个循环,以便它遍历所有索引):

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. 它说arr [0] .filter不是函数,但是不知道为什么认为arr [0]应该作为函数考虑。

When I remove the [0] it gives back a blank array... 当我删除[0]时,它会返回一个空白数组...

So my question is how can I get the .filter function to give back the complete object based on tags in the tags array. 所以我的问题是如何获取.filter函数以基于标签数组中的标签来返回完整的对象。 So that if I search for the tag "school" it gives back arr[1] and arr[2], the complete objects. 这样,如果我搜索标签“ school”,它会返回完整的对象arr [1]和arr [2]。

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! 希望你们能帮助我!

Refering to the documention : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?v=control 参考文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?v= control

you shall use : 您应使用:

var arr = new Array;

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


var toFind = "offline";
//I'm filtering on arr[0].tags directly !! 
var filtered = arr[0].tags.filter(function(el) { 

  return el === toFind;
});

console.log(filtered);

Like has been said, arr[0] points to an object, which doesn't have a filter method. 就像已经说过的那样,arr [0]指向一个没有过滤方法的对象。 You need to call filter on the array itself 您需要在数组本身上调用filter

 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); 

If I am interpreting your question correctly, it appears that you want something along the lines of 如果我正确地解释了您的问题,则似乎您想要一些与

arr.forEach((a) => {
    if (a["tags"].includes(toFind))
        console.log(a);
});

This will loop through each element of arr and check that the tags array contains toFind (and prints out the object if it does). 这将遍历arr每个元素,并检查tags数组是否包含toFind (如果存在,则将其打印出来)。

if you want to do it for the whole array then try this 如果您想对整个阵列进行操作,请尝试此操作

 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(el) {
  return el.tags.find(function (entity){
    return entity===toFind;
  });
});

 console.log(filtered);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将包含对象的object转换为包含对象的数组 - Convert object containing objects to array containing objects 包含对象的对象数组到平面 object 数组 - Array of objects containing objects to flat object array 根据包含来自另一个数组的所有值的数组属性过滤对象数组 - Filter an array of objects based on array property containing all values from another array setState 包含对象=>数组=> 对象 - setState containing object=>array=> objects 使用字符串数组过滤包含包含对象数组的键的 object - Filter an object containing a key that contains an array of objects using an array of strings 根据另一个包含 angular js 对象的数组过滤包含对象的数组 - Filter an array containing objects based on another array containing objects in angular js 如何将包含数组的对象转换为对象的对象 - How to convert objects containing array into object of objects 如何将包含对象的 object 转换为对象数组 - How to convert object containing Objects into array 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? 对包含对象数组的 Object 进行过滤和排序,同时保留空 Arrays - Filter and Sort an Object Containing Array of Objects While Retaining Empty Arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM