So I have data like the following:
[
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
{
"id": 1,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Architecture", "label": "Architecture"}],
"items": []
},
]
I'm trying to filter through an array and only return if the tags array contains a value that is ==
to another string.
This is what I came up with but still seems to be sending back the whole array:
const tagMoodboards = _moodboards.filter(mb => { return mb.tags.filter(t => t.value == name) })
So I have data like the following:
[
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
{
"id": 1,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Architecture", "label": "Architecture"}],
"items": []
},
]
I'm trying to filter through an array and only return if the tags array contains a value that is ==
to another string.
This is what I came up with but still seems to be sending back the whole array:
const tagMoodboards = _moodboards.filter(mb => { return mb.tags.filter(t => t.value == name) })
Share
Improve this question
asked Jul 2, 2018 at 2:15
DileetDileet
2,0743 gold badges36 silver badges59 bronze badges
5
-
You need to use
map()
to return clones with filtered arrays. – SLaks Commented Jul 2, 2018 at 2:16 -
2
What is the desired result and what's in
mb.tags
? I don't follow what the objective is. – jfriend00 Commented Jul 2, 2018 at 2:17 -
2
If i understand the question right, the inner part looks more like an application for
some
. – ASDFGerte Commented Jul 2, 2018 at 2:19 -
2
mb.tags.filter(t => t.value == name)
will always be an array, so it’s always truthy, so the outerfilter
is equivalent to justconst tagMoodboards = _moodboards.filter(mb => { return true })
. – Sebastian Simon Commented Jul 2, 2018 at 2:20 - 1 Although filter() not best approach on the inner .... return the length of the resultant array instead of the array itself – charlietfl Commented Jul 2, 2018 at 2:22
2 Answers
Reset to default 9You don't want a filter
inside a filter
- rather, inside the filter, check if some
of the tags
objects have the .value
property that you want
const _moodboards = [
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
{
"id": 1,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Architecture", "label": "Architecture"}],
"items": []
},
];
const name = 'Architecture';
console.log(_moodboards.filter(({ tags }) => (
tags.some(({ value }) => value === name)
)));
To use filter()
you need something that will return true or false -- i.e. a Boolean. That should be the first place you start. So given an object like
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
if you want to decide whether or not that should be used, you might try Array.some()
on the tags
array. This will return a boolean.
let tags = [{"value": "Art", "label": "Art"}]
console.log(tags.some(tag => tag.value = "Art")) // true
With that in hand, you can now use filter()
and some()
together. some()
will return true or false for each item in the array and that will determine whether it's filtered or not:
let arr = [{"id": 0,"title": "happy dayys","owner": {"id": "1", "username": "dillonraphael"},"tags": [{"value": "Art", "label": "Art"}],"items": []},{"id": 1,"title": "happy dayys","owner": {"id": "1", "username": "dillonraphael"},"tags": [{"value": "Architecture", "label": "Architecture"}],"items": []},]
console.log(arr.filter(obj => obj.tags.some(o => o.value == 'Art') ))