find element in array in array of objects
I'm trying to return the object that contains the value (any letter matching in any object) in search field.
I want to return the object that the array inside an array of object matches.
Example:
if
search = 'dog'
then I want to return
[{name: Jake, last: DK, tags: ['fun','dog','cat']}]
Or if
search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]
My attempt:
this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})
Also, I have
if (obj.tags !== undefined) {
because its a list that appears on click of a button and its undefined initially when page render.
javascript reactjs
add a comment |
I'm trying to return the object that contains the value (any letter matching in any object) in search field.
I want to return the object that the array inside an array of object matches.
Example:
if
search = 'dog'
then I want to return
[{name: Jake, last: DK, tags: ['fun','dog','cat']}]
Or if
search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]
My attempt:
this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})
Also, I have
if (obj.tags !== undefined) {
because its a list that appears on click of a button and its undefined initially when page render.
javascript reactjs
.filter()
always returns an array. And an array is truthy. HencefilteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (whenstudent.tags
is notundefined
).
– Andreas
Nov 22 at 18:20
Even after your last edit, you still refer tostudent
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
– Mark Adelsberger
Nov 22 at 18:24
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
1
What is your question?
– Andy Ray
Nov 22 at 18:26
add a comment |
I'm trying to return the object that contains the value (any letter matching in any object) in search field.
I want to return the object that the array inside an array of object matches.
Example:
if
search = 'dog'
then I want to return
[{name: Jake, last: DK, tags: ['fun','dog','cat']}]
Or if
search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]
My attempt:
this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})
Also, I have
if (obj.tags !== undefined) {
because its a list that appears on click of a button and its undefined initially when page render.
javascript reactjs
I'm trying to return the object that contains the value (any letter matching in any object) in search field.
I want to return the object that the array inside an array of object matches.
Example:
if
search = 'dog'
then I want to return
[{name: Jake, last: DK, tags: ['fun','dog','cat']}]
Or if
search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]
My attempt:
this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})
Also, I have
if (obj.tags !== undefined) {
because its a list that appears on click of a button and its undefined initially when page render.
javascript reactjs
javascript reactjs
edited Nov 22 at 18:35
josh21
105312
105312
asked Nov 22 at 18:16
Binny.H
386
386
.filter()
always returns an array. And an array is truthy. HencefilteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (whenstudent.tags
is notundefined
).
– Andreas
Nov 22 at 18:20
Even after your last edit, you still refer tostudent
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
– Mark Adelsberger
Nov 22 at 18:24
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
1
What is your question?
– Andy Ray
Nov 22 at 18:26
add a comment |
.filter()
always returns an array. And an array is truthy. HencefilteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (whenstudent.tags
is notundefined
).
– Andreas
Nov 22 at 18:20
Even after your last edit, you still refer tostudent
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
– Mark Adelsberger
Nov 22 at 18:24
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
1
What is your question?
– Andy Ray
Nov 22 at 18:26
.filter()
always returns an array. And an array is truthy. Hence filteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags
is not undefined
).– Andreas
Nov 22 at 18:20
.filter()
always returns an array. And an array is truthy. Hence filteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags
is not undefined
).– Andreas
Nov 22 at 18:20
Even after your last edit, you still refer to
student
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)– Mark Adelsberger
Nov 22 at 18:24
Even after your last edit, you still refer to
student
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)– Mark Adelsberger
Nov 22 at 18:24
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
1
1
What is your question?
– Andy Ray
Nov 22 at 18:26
What is your question?
– Andy Ray
Nov 22 at 18:26
add a comment |
5 Answers
5
active
oldest
votes
The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
add a comment |
You need to return true
or false
in the callback function of filter
. For example, student.tags.filter(
has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => {
does not return a boolean. Here's how it should be changed:
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})
add a comment |
You can use a combination of filter
and some
to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)
):
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
add a comment |
in this method, you return many elements that have the same tag:
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
add a comment |
This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some()
is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436367%2ffind-element-in-array-in-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
add a comment |
The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
add a comment |
The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})
The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})
edited Nov 22 at 18:34
answered Nov 22 at 18:25
anuragb26
16214
16214
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
add a comment |
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
Follow up question, what if I wanted to also filter by first name or last name?
– Binny.H
Nov 22 at 19:00
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
– anuragb26
Nov 23 at 6:17
add a comment |
You need to return true
or false
in the callback function of filter
. For example, student.tags.filter(
has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => {
does not return a boolean. Here's how it should be changed:
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})
add a comment |
You need to return true
or false
in the callback function of filter
. For example, student.tags.filter(
has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => {
does not return a boolean. Here's how it should be changed:
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})
add a comment |
You need to return true
or false
in the callback function of filter
. For example, student.tags.filter(
has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => {
does not return a boolean. Here's how it should be changed:
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})
You need to return true
or false
in the callback function of filter
. For example, student.tags.filter(
has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => {
does not return a boolean. Here's how it should be changed:
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})
answered Nov 22 at 18:26
Kevin Bai
18817
18817
add a comment |
add a comment |
You can use a combination of filter
and some
to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)
):
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
add a comment |
You can use a combination of filter
and some
to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)
):
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
add a comment |
You can use a combination of filter
and some
to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)
):
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
You can use a combination of filter
and some
to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)
):
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
function search(term, arr) {
return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
}
var data = [{
name: 'Jake',
last: 'DK',
tags: ['fun', 'dog', 'cat']
},
{
name: 'John',
last: 'Hop',
tags: ['boring', 'mouse', 'trap']
}
];
console.log(search('dog', data));
console.log(search('a', data));
console.log(search('dog', undefined));
edited Nov 22 at 18:33
answered Nov 22 at 18:28
slider
8,0451129
8,0451129
add a comment |
add a comment |
in this method, you return many elements that have the same tag:
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
add a comment |
in this method, you return many elements that have the same tag:
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
add a comment |
in this method, you return many elements that have the same tag:
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
in this method, you return many elements that have the same tag:
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
{name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];
function search(searchText){
var ret =
arr.forEach(function (element){
if(element.tags.includes(searchText)){
ret.push(element)
}
})
return ret;
}
console.log(search("dog"));
answered Nov 22 at 18:36
Demian
1637
1637
add a comment |
add a comment |
This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some()
is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
add a comment |
This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some()
is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
add a comment |
This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some()
is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some()
is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
{name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]
let term = 'dog'
let found = arr.filter(item =>
/* return a boolean here */
/* && will short circuit returning false if there's no tags */
/* some will return true if any word contains term */
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
// also works with `a`
term = 'a'
found = arr.filter(item =>
item.tags && item.tags.some(word => word.includes(term))
)
console.log(found)
edited Nov 22 at 18:43
answered Nov 22 at 18:31
Mark Meyer
35.1k32855
35.1k32855
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436367%2ffind-element-in-array-in-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
.filter()
always returns an array. And an array is truthy. HencefilteredTag
(which btw. is a terrible name when the actual content is an array of students) will always have all students (whenstudent.tags
is notundefined
).– Andreas
Nov 22 at 18:20
Even after your last edit, you still refer to
student
without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)– Mark Adelsberger
Nov 22 at 18:24
@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25
1
What is your question?
– Andy Ray
Nov 22 at 18:26