I have a json file in which I have to find a string is available of not.
Example:
let testjson =require('./jsontest.json');
let string= 'abcd';
I would like to find if abcd
in json object.
My json file looks like following
[
{
name:"aaaa",
j1:[ "abcd","efg"],
j2:[ "abcde","xyz"]
},
{
name:"bbbbb",
j1:[ "abcdw","efg1"],
j2:[ "abcde12","xyz22"]
}
]
Please let me know how to fix this issue in Node js
2
Answers
You can use
filter
orsome
method to find the givenstring
is present or not in the array.Major difference between these method is
filter
return the matchedobject
or an empty array if no match andsome
returntrue
if any one one of item matches the condition orfalse
.More on MDN
The example that you have given have objects and string in the json file. You can use a recursive function to search for a string within a
JSON object
and you can useincludes
to check if if has the string or not, something like:replace
testjson
with your JSON object andsearchString
with the string you want to search for in your json .