Filtering array so it would remove all undefined objects
I need to filter an array so it would remove undefined objects from it.
I tried with lodash _.filter but didn't succeed (returned completely empty array)
_.filter(myArray, _.isEmpty)
I'm using Angular 6 so anything with typescript or lodash would be perfect.
arrays angular object lodash
add a comment |
I need to filter an array so it would remove undefined objects from it.
I tried with lodash _.filter but didn't succeed (returned completely empty array)
_.filter(myArray, _.isEmpty)
I'm using Angular 6 so anything with typescript or lodash would be perfect.
arrays angular object lodash
add a comment |
I need to filter an array so it would remove undefined objects from it.
I tried with lodash _.filter but didn't succeed (returned completely empty array)
_.filter(myArray, _.isEmpty)
I'm using Angular 6 so anything with typescript or lodash would be perfect.
arrays angular object lodash
I need to filter an array so it would remove undefined objects from it.
I tried with lodash _.filter but didn't succeed (returned completely empty array)
_.filter(myArray, _.isEmpty)
I'm using Angular 6 so anything with typescript or lodash would be perfect.
arrays angular object lodash
arrays angular object lodash
asked Nov 12 at 9:01
raulicious
941212
941212
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
An easier way:
_.filter(myArray, Boolean)
This rids the array of nulls, 0's and undefined's.
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
add a comment |
The more simple way
_.filter(myArray, function(o) { return o !== undefined });
add a comment |
Using Javascript also feasible. it supports null,undefined, 0, empty.
newArray = myArray.filter(item=> item);
add a comment |
You don't need a library; the Javascript array type has a filter
method:
var filteredArray = myArray.filter(item => item !== undefined);
add a comment |
In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)
add a comment |
var newArray = array.filter(arrayItem => arrayItem)
It will Filtering array so it would remove all undefined objects and assign to new variable called newArray
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
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%2f53258766%2ffiltering-array-so-it-would-remove-all-undefined-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
An easier way:
_.filter(myArray, Boolean)
This rids the array of nulls, 0's and undefined's.
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
add a comment |
An easier way:
_.filter(myArray, Boolean)
This rids the array of nulls, 0's and undefined's.
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
add a comment |
An easier way:
_.filter(myArray, Boolean)
This rids the array of nulls, 0's and undefined's.
An easier way:
_.filter(myArray, Boolean)
This rids the array of nulls, 0's and undefined's.
answered Nov 12 at 9:04
rrd
2,83831222
2,83831222
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
add a comment |
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
Thanks a lot, since this is the cleanest way I will accept this answer once I can.
– raulicious
Nov 12 at 9:08
add a comment |
The more simple way
_.filter(myArray, function(o) { return o !== undefined });
add a comment |
The more simple way
_.filter(myArray, function(o) { return o !== undefined });
add a comment |
The more simple way
_.filter(myArray, function(o) { return o !== undefined });
The more simple way
_.filter(myArray, function(o) { return o !== undefined });
edited Nov 12 at 9:04
Ayush Gupta
3,0261643
3,0261643
answered Nov 12 at 9:03
firegloves
3,21511025
3,21511025
add a comment |
add a comment |
Using Javascript also feasible. it supports null,undefined, 0, empty.
newArray = myArray.filter(item=> item);
add a comment |
Using Javascript also feasible. it supports null,undefined, 0, empty.
newArray = myArray.filter(item=> item);
add a comment |
Using Javascript also feasible. it supports null,undefined, 0, empty.
newArray = myArray.filter(item=> item);
Using Javascript also feasible. it supports null,undefined, 0, empty.
newArray = myArray.filter(item=> item);
answered Nov 12 at 9:05
Suresh Kumar Ariya
4,4131215
4,4131215
add a comment |
add a comment |
You don't need a library; the Javascript array type has a filter
method:
var filteredArray = myArray.filter(item => item !== undefined);
add a comment |
You don't need a library; the Javascript array type has a filter
method:
var filteredArray = myArray.filter(item => item !== undefined);
add a comment |
You don't need a library; the Javascript array type has a filter
method:
var filteredArray = myArray.filter(item => item !== undefined);
You don't need a library; the Javascript array type has a filter
method:
var filteredArray = myArray.filter(item => item !== undefined);
answered Nov 12 at 9:08
stone
4,5303859
4,5303859
add a comment |
add a comment |
In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)
add a comment |
In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)
add a comment |
In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)
In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)
edited Nov 12 at 9:39
answered Nov 12 at 9:32
Koushik Chatterjee
2,73731024
2,73731024
add a comment |
add a comment |
var newArray = array.filter(arrayItem => arrayItem)
It will Filtering array so it would remove all undefined objects and assign to new variable called newArray
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
add a comment |
var newArray = array.filter(arrayItem => arrayItem)
It will Filtering array so it would remove all undefined objects and assign to new variable called newArray
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
add a comment |
var newArray = array.filter(arrayItem => arrayItem)
It will Filtering array so it would remove all undefined objects and assign to new variable called newArray
var newArray = array.filter(arrayItem => arrayItem)
It will Filtering array so it would remove all undefined objects and assign to new variable called newArray
edited Nov 12 at 11:04
answered Nov 12 at 9:16
ishani shah
364
364
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
add a comment |
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 12 at 10:25
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%2f53258766%2ffiltering-array-so-it-would-remove-all-undefined-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