У меня есть массив объектов, который выглядит примерно так:Как Переберите массив объектов и проверить, если значение соответствует либо строку в объект или значение в массиве элементов с помощью lodash
[{
"title": "first footer",
"section": "structure",
"categoryId": "footer",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wobble"],
"uId": "footerA"
},{
"title": "second footer",
"section": "structure",
"categoryId": "footer",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "footerB"
},
{
"title": "first header",
"section": "structure",
"categoryId": "header",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "headerA"
},
{
"title": "second header",
"section": "structure",
"categoryId": "header",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "headerB"
},
{
"title": "first landing",
"section": "structure",
"categoryId": "landing",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "landingA"
},
{
"title": "second landing",
"section": "structure",
"categoryId": "landing",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "landingB"
},
{
"title": "third landing",
"section": "structure",
"categoryId": "landing",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "landingC"
},
{
"title": "first nav",
"section": "structure",
"categoryId": "navigation",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wobble"],
"uId": "navA"
},{
"title": "first blog",
"section": "components",
"categoryId": "blog",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "blogA"
},
{
"title": "second blog sdf wicked",
"section": "components",
"categoryId": "blog",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "blogB"
},
{
"title": "first header",
"section": "components",
"categoryId": "contact_button",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "contact_buttonA"
},
{
"title": "first landing",
"section": "components",
"categoryId": "content_bloc",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "content_blocA"
},
{
"title": "second landing",
"section": "components",
"categoryId": "content_bloc",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "content_blocB"
},
{
"title": "third landing",
"section": "components",
"categoryId": "content_bloc",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "content_blocC"
},
{
"title": "first nav",
"section": "components",
"categoryId": "cover",
"image": "http://placehold.it/350x220",
"hashtags": ["#popin", "#wibble"],
"uId": "coverA"
}]
Когда я нажимаю кнопку, я хочу, чтобы иметь возможность фильтровать объекты в массиве, чтобы возвращать только согласованные объекты, которые кто-то искал. Например, если кто-то набрал «землю», функция должна проверить, существует ли «земля» в названии компонента или существует в массиве hashtags этого компонента.
Я использую lodash для этого, поэтому хотел бы продолжать использовать это.
Я смог выполнить тест для названия, но зациклился на том, как циклически перебирать массив hashtags при просмотре одного компонента в цикле.
Это мой код до сих пор:
_.filter(this.components, function (component) {
//Check if components title has 'wic' in the text or if 'wic' exists in the hashtags array
if(_.includes(component.title, 'wic')) {
console.log(component);
}
});
Привет, это работает, но было интересно, можем ли мы использовать lodash, поскольку я использовал его на других моих страницах и хочу сохранить согласованность. – saunders
@saunders _ «это действительно работает, но было интересно, можем ли мы использовать lodash» _ Почему нужна библиотека? '.filter()' и '.some()' определены в 'Array.prototype' – guest271314
Я изучаю loadash и хочу сохранить согласованность в том, как я выполняю эти типы функций, поскольку у меня уже есть другие, написанные на lodash, поэтому хочу сохранить согласованность – saunders