P
Peter May
I have a simple JSON:
var j = '{
"result": "1",
"get": [
{
"type": "test",
"more": {
"type": "test1",
},
"more2": {
"qwe": "test2",
"email": null,
},
"more3": null
},
{
"type": "best",
"more": {
"type": "test11",
},
"more2": {
"qwe": "test22",
"email": null,
},
"more3": null
},
]
}';
var res = JSON.parse(j);
var search_value = 'test22'; // for example search text
I want to filter (in the above example) array in object "get" that the
final result will only leave this objects that contains any property
which have a search_value text.
In above example the final result will be:
var results = {
"result": "1",
"get": [
{
"type": "best",
"more": {
"type": "test11",
},
"more2": {
"qwe": "test22",
"email": null,
},
"more3": null
},
]
};
I can do this using for-in loop, but if there will be more objects in
object then building every time a new for-in loop is not a good idea,
for me.
The question is: is there a way to iterate all objects (using one
method) and find only that objects which contains search_value in any
property?
var j = '{
"result": "1",
"get": [
{
"type": "test",
"more": {
"type": "test1",
},
"more2": {
"qwe": "test2",
"email": null,
},
"more3": null
},
{
"type": "best",
"more": {
"type": "test11",
},
"more2": {
"qwe": "test22",
"email": null,
},
"more3": null
},
]
}';
var res = JSON.parse(j);
var search_value = 'test22'; // for example search text
I want to filter (in the above example) array in object "get" that the
final result will only leave this objects that contains any property
which have a search_value text.
In above example the final result will be:
var results = {
"result": "1",
"get": [
{
"type": "best",
"more": {
"type": "test11",
},
"more2": {
"qwe": "test22",
"email": null,
},
"more3": null
},
]
};
I can do this using for-in loop, but if there will be more objects in
object then building every time a new for-in loop is not a good idea,
for me.
The question is: is there a way to iterate all objects (using one
method) and find only that objects which contains search_value in any
property?