Filter selected children from data using Javascript
I try to write a function in JavaScript which filter an array by a selected property (an value).
But it works for 2 level only I do not understand what do I missing.
The data I want to filter:
var data = [
name: "john_pc",
children: [
name: "sabrina_pc",
children: [
name: "sabrina_pc"
,
name: "john_pc"
]
,
name: "john_pc"
]
,
name: "sabrina_pc"
]
The childrenFilter funciton :
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters)
var optionalValues = filters[property];
var value = item[property];
if (item.children)
item.children = childrenFilter(item.children, filters);
let hasValue = value == optionalValues;
if (hasValue)
return true;
return false;
return false;
, this);
return filteredData;
Calling the function:
As you can see the 'childrenFilter' get an object which the key is property in the data and the key is value I want to keep.
let result = childrenFilter(data,
"name": "a1"
);
console.log(JSON.stringify(result, null, 2))
The wanted result :
[
"name": "john_pc",
"children": [
"name": "sabrina_pc",
"children": [
"name": "john_pc"
]
,
"name": "john_pc"
]
]
javascript data-structures filter
add a comment |
I try to write a function in JavaScript which filter an array by a selected property (an value).
But it works for 2 level only I do not understand what do I missing.
The data I want to filter:
var data = [
name: "john_pc",
children: [
name: "sabrina_pc",
children: [
name: "sabrina_pc"
,
name: "john_pc"
]
,
name: "john_pc"
]
,
name: "sabrina_pc"
]
The childrenFilter funciton :
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters)
var optionalValues = filters[property];
var value = item[property];
if (item.children)
item.children = childrenFilter(item.children, filters);
let hasValue = value == optionalValues;
if (hasValue)
return true;
return false;
return false;
, this);
return filteredData;
Calling the function:
As you can see the 'childrenFilter' get an object which the key is property in the data and the key is value I want to keep.
let result = childrenFilter(data,
"name": "a1"
);
console.log(JSON.stringify(result, null, 2))
The wanted result :
[
"name": "john_pc",
"children": [
"name": "sabrina_pc",
"children": [
"name": "john_pc"
]
,
"name": "john_pc"
]
]
javascript data-structures filter
2
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33
add a comment |
I try to write a function in JavaScript which filter an array by a selected property (an value).
But it works for 2 level only I do not understand what do I missing.
The data I want to filter:
var data = [
name: "john_pc",
children: [
name: "sabrina_pc",
children: [
name: "sabrina_pc"
,
name: "john_pc"
]
,
name: "john_pc"
]
,
name: "sabrina_pc"
]
The childrenFilter funciton :
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters)
var optionalValues = filters[property];
var value = item[property];
if (item.children)
item.children = childrenFilter(item.children, filters);
let hasValue = value == optionalValues;
if (hasValue)
return true;
return false;
return false;
, this);
return filteredData;
Calling the function:
As you can see the 'childrenFilter' get an object which the key is property in the data and the key is value I want to keep.
let result = childrenFilter(data,
"name": "a1"
);
console.log(JSON.stringify(result, null, 2))
The wanted result :
[
"name": "john_pc",
"children": [
"name": "sabrina_pc",
"children": [
"name": "john_pc"
]
,
"name": "john_pc"
]
]
javascript data-structures filter
I try to write a function in JavaScript which filter an array by a selected property (an value).
But it works for 2 level only I do not understand what do I missing.
The data I want to filter:
var data = [
name: "john_pc",
children: [
name: "sabrina_pc",
children: [
name: "sabrina_pc"
,
name: "john_pc"
]
,
name: "john_pc"
]
,
name: "sabrina_pc"
]
The childrenFilter funciton :
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters)
var optionalValues = filters[property];
var value = item[property];
if (item.children)
item.children = childrenFilter(item.children, filters);
let hasValue = value == optionalValues;
if (hasValue)
return true;
return false;
return false;
, this);
return filteredData;
Calling the function:
As you can see the 'childrenFilter' get an object which the key is property in the data and the key is value I want to keep.
let result = childrenFilter(data,
"name": "a1"
);
console.log(JSON.stringify(result, null, 2))
The wanted result :
[
"name": "john_pc",
"children": [
"name": "sabrina_pc",
"children": [
"name": "john_pc"
]
,
"name": "john_pc"
]
]
javascript data-structures filter
javascript data-structures filter
edited Nov 13 '18 at 18:45
E1ad
asked Nov 13 '18 at 18:18
E1adE1ad
325
325
2
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33
add a comment |
2
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33
2
2
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33
add a comment |
2 Answers
2
active
oldest
votes
Your filter function does not take into account whether or not children elements match the pattern, therefore even though some child elements of the object match the pattern, the object itself is being filtered out.
Here is the explanation:
name: "a2", // does not match filter name:'a1 so is removed alongside child objects
children: [ // gets removed with parent object
name: "a2"
,
name: "a1"
]
This should produce the desired output:
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters) item.children.length) // include item when children mathes the pattern
return true;
return false;
return false;
, this);
return filteredData;
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
add a comment |
You could build new array for each step of filtering, beginning from the leaves and check if this contains the wanted value.
This approach generates new objects and does not mutate the original data.
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
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%2f53287241%2ffilter-selected-children-from-data-using-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your filter function does not take into account whether or not children elements match the pattern, therefore even though some child elements of the object match the pattern, the object itself is being filtered out.
Here is the explanation:
name: "a2", // does not match filter name:'a1 so is removed alongside child objects
children: [ // gets removed with parent object
name: "a2"
,
name: "a1"
]
This should produce the desired output:
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters) item.children.length) // include item when children mathes the pattern
return true;
return false;
return false;
, this);
return filteredData;
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
add a comment |
Your filter function does not take into account whether or not children elements match the pattern, therefore even though some child elements of the object match the pattern, the object itself is being filtered out.
Here is the explanation:
name: "a2", // does not match filter name:'a1 so is removed alongside child objects
children: [ // gets removed with parent object
name: "a2"
,
name: "a1"
]
This should produce the desired output:
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters) item.children.length) // include item when children mathes the pattern
return true;
return false;
return false;
, this);
return filteredData;
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
add a comment |
Your filter function does not take into account whether or not children elements match the pattern, therefore even though some child elements of the object match the pattern, the object itself is being filtered out.
Here is the explanation:
name: "a2", // does not match filter name:'a1 so is removed alongside child objects
children: [ // gets removed with parent object
name: "a2"
,
name: "a1"
]
This should produce the desired output:
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters) item.children.length) // include item when children mathes the pattern
return true;
return false;
return false;
, this);
return filteredData;
Your filter function does not take into account whether or not children elements match the pattern, therefore even though some child elements of the object match the pattern, the object itself is being filtered out.
Here is the explanation:
name: "a2", // does not match filter name:'a1 so is removed alongside child objects
children: [ // gets removed with parent object
name: "a2"
,
name: "a1"
]
This should produce the desired output:
const childrenFilter = (childrenData, filters) =>
let filteredData = childrenData.filter(item =>
for (var property in filters) item.children.length) // include item when children mathes the pattern
return true;
return false;
return false;
, this);
return filteredData;
edited Nov 13 '18 at 18:45
answered Nov 13 '18 at 18:38
MateuszMateusz
263
263
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
add a comment |
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
Right... How do you recommend to fix that?
– E1ad
Nov 13 '18 at 18:46
add a comment |
You could build new array for each step of filtering, beginning from the leaves and check if this contains the wanted value.
This approach generates new objects and does not mutate the original data.
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
You could build new array for each step of filtering, beginning from the leaves and check if this contains the wanted value.
This approach generates new objects and does not mutate the original data.
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
You could build new array for each step of filtering, beginning from the leaves and check if this contains the wanted value.
This approach generates new objects and does not mutate the original data.
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
You could build new array for each step of filtering, beginning from the leaves and check if this contains the wanted value.
This approach generates new objects and does not mutate the original data.
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
function filter(array, filters)
return array.reduce((r, o) => , filters);
return children , undefined);
var data = [ name: "a1", children: [ name: "a2", children: [ name: "a2" , name: "a1" ] , name: "a1" ] , name: "b1" ];
console.log(filter(data, name: "a1" ));
.as-console-wrapper max-height: 100% !important; top: 0;
edited Nov 13 '18 at 19:06
answered Nov 13 '18 at 18:53
Nina ScholzNina Scholz
181k1494163
181k1494163
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.
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%2f53287241%2ffilter-selected-children-from-data-using-javascript%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
2
please add the wanted result as well. please choose unique names.
– Nina Scholz
Nov 13 '18 at 18:21
For an object to be included, does it have to match all the filters or just one of them?
– slider
Nov 13 '18 at 18:28
It is a good question.Maybe I should add third argument to do both
– E1ad
Nov 13 '18 at 18:32
can you show a more realistic example instead of a bunch of a1 and a2?
– PA.
Nov 13 '18 at 18:33
If you could post what your wanting as a result that would really help us to assist you. Also when posting a recursive function comments and specific naming really helps. It may be clear to you, but to anyone who didn't write it, this particular recursive function may be difficult to wrap their head around.
– zfrisch
Nov 13 '18 at 18:33