Deep filtering in hierarchical object
I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.
For example I have an object actions
as below. I want to filter out those that have an default
property which is set to true
.
let actions={
sql: [
{
content: 'aaa',
default: true
},
{
stmt: 'bbb'
},
{
stmt: 'ccc',
default: true
}],
email: [
{
content: 'xxx',
default: true
}]
}
So eventually I want to have something like:
let filteredActions={
sql: [
{
stmt: 'bbb'
}
],
email:
}
This how I try to solve my problem, but seems it does not work.
getNonDefaultActions = actions =>
map(actions, (actionListByType, key) => {
actionListByType.filter(action => !(action.defaultFlag === true))
});
It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great
javascript node.js lodash
add a comment |
I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.
For example I have an object actions
as below. I want to filter out those that have an default
property which is set to true
.
let actions={
sql: [
{
content: 'aaa',
default: true
},
{
stmt: 'bbb'
},
{
stmt: 'ccc',
default: true
}],
email: [
{
content: 'xxx',
default: true
}]
}
So eventually I want to have something like:
let filteredActions={
sql: [
{
stmt: 'bbb'
}
],
email:
}
This how I try to solve my problem, but seems it does not work.
getNonDefaultActions = actions =>
map(actions, (actionListByType, key) => {
actionListByType.filter(action => !(action.defaultFlag === true))
});
It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great
javascript node.js lodash
add a comment |
I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.
For example I have an object actions
as below. I want to filter out those that have an default
property which is set to true
.
let actions={
sql: [
{
content: 'aaa',
default: true
},
{
stmt: 'bbb'
},
{
stmt: 'ccc',
default: true
}],
email: [
{
content: 'xxx',
default: true
}]
}
So eventually I want to have something like:
let filteredActions={
sql: [
{
stmt: 'bbb'
}
],
email:
}
This how I try to solve my problem, but seems it does not work.
getNonDefaultActions = actions =>
map(actions, (actionListByType, key) => {
actionListByType.filter(action => !(action.defaultFlag === true))
});
It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great
javascript node.js lodash
I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.
For example I have an object actions
as below. I want to filter out those that have an default
property which is set to true
.
let actions={
sql: [
{
content: 'aaa',
default: true
},
{
stmt: 'bbb'
},
{
stmt: 'ccc',
default: true
}],
email: [
{
content: 'xxx',
default: true
}]
}
So eventually I want to have something like:
let filteredActions={
sql: [
{
stmt: 'bbb'
}
],
email:
}
This how I try to solve my problem, but seems it does not work.
getNonDefaultActions = actions =>
map(actions, (actionListByType, key) => {
actionListByType.filter(action => !(action.defaultFlag === true))
});
It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great
javascript node.js lodash
javascript node.js lodash
asked Jan 1 at 18:54


RobertRobert
52
52
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use lodash's _.mapValue()
with _.reject()
:
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
add a comment |
You could get a new object with filtered properties.
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
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%2f53998079%2fdeep-filtering-in-hierarchical-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use lodash's _.mapValue()
with _.reject()
:
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can use lodash's _.mapValue()
with _.reject()
:
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can use lodash's _.mapValue()
with _.reject()
:
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
You can use lodash's _.mapValue()
with _.reject()
:
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}
const result = _.mapValues(actions, arr => _.reject(arr, 'default'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
answered Jan 1 at 18:57


Ori DroriOri Drori
79.6k138797
79.6k138797
add a comment |
add a comment |
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
add a comment |
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
add a comment |
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};
Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));
console.log(actions);
answered Jan 1 at 19:04
ggorlenggorlen
7,5033826
7,5033826
add a comment |
add a comment |
You could get a new object with filtered properties.
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
add a comment |
You could get a new object with filtered properties.
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
add a comment |
You could get a new object with filtered properties.
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
You could get a new object with filtered properties.
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
result = Object.assign(
...Object
.entries(actions)
.map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
);
console.log(result);
answered Jan 1 at 19:05


Nina ScholzNina Scholz
191k15100174
191k15100174
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
add a comment |
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
Happy new year :)
– Ori Drori
Jan 1 at 19:25
Happy new year :)
– Ori Drori
Jan 1 at 19:25
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
@OriDrori, happy new year to you, too :)
– Nina Scholz
Jan 1 at 20:47
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%2f53998079%2fdeep-filtering-in-hierarchical-object%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