Javascript 使用下划线js从json对象数组中筛选

Javascript 使用下划线js从json对象数组中筛选,javascript,json,underscore.js,Javascript,Json,Underscore.js,因此,“collections”基本上是一个json对象数组,其中每个对象都包含一个属性,“categories”包含id,如82、84等。因此我想过滤“collections”对象,其类别为82(输入参数:“category_id”),并使用下划线js将其存储到变量中。(P.S-使用.filter)据我所知,.filter方法与Array.filter方法的作用相同。不确定是否需要在此处使用下划线 function filterCollections(collectionsArray, cat


因此,“collections”基本上是一个json对象数组,其中每个对象都包含一个属性,“categories”包含id,如82、84等。因此我想过滤“collections”对象,其类别为82(输入参数:“category_id”),并使用下划线js将其存储到变量中。(P.S-使用.filter)

据我所知,.filter方法与Array.filter方法的作用相同。不确定是否需要在此处使用下划线

function filterCollections(collectionsArray, categoryId) {
    return collectionsArray.filter((collection) => {
        return collection.categories.indexOf(categoryId) > -1;
    })
}
使用下划线应该是这样的:

function filterCollections(collectionsArray, categoryId) {
    return _.filter(collectionsArray, (collection) => {
        return collection.categories.indexOf(categoryId) > -1;
    })
}
如果输入是json字符串,则需要使用json.parse()将其解析为数组


所以,在这些问题上,被问到最多的问题是,到目前为止你尝试了什么,你被困在哪里了?我在我犯错误的地方得到了它。现在是过滤。非常感谢。
collectionsArray = JSON.parse(jsonString);