Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm using the filter method to return an object property when a value is true. However my filter method doesn't stop iterating over the array when it finds the true value so iterates over all the elements and returns a null value.
I want to break out of the array filter once the condition is true.
This is what I've done:
array.filter((v)=>{
var a = v.id === x ? v.collection : null
console.log(a)
return a
})
I have three elements in the array and my console.log prints out 'music', 'null', 'null'. I want it to break when it is 'music'.
If you only want the first matching element, you can use find rather than filter.
It will return the first matching element, stop iterating, and return the match.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
const arrFilter = [{id:1},[],[]];
how can I filter the empty array in the above example, so that the result would be the object with an id only
You can use the length of the array returned from Object.keys() to determine if objects, arrays or strings are empty however it won't work on numbers.
With that in mind, try this assuming that
all empty objects, arrays and strings should be omitted
everything else stays
const arrFilter = [{id:1},[],[], "a string", "", 1, 0];
const nonEmpties = arrFilter.filter(
(item) => typeof item === "number" || Object.keys(item).length > 0
);
console.log(nonEmpties);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I have explore a few webs and watch some videos but I couldn't understand how array.forEach() works
This is the script that I found regarding that
let meals = ["rice", "meat", "salad"];
meals.forEach(capitalize);
meals.forEach(print);
function capitalize(element, index, array){
array[index] = element[0].toUpperCase() + element.substring(1);
}
function print(element){
console.log(element);
}
Can someone explain to me about how array[index] = element(0).toUpperCase() + element.substring(1); and meal.forEach(); works?
forEach() is an array function from JavaScript, that is used to iterate over items in a given array.
element[0] will return first character of each array element.
toUpperCase() is function use to convertthe string into upper case.
element.substring(1) this will return a string, except for the first character.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can I remove Empty item such as Item A2[]
List = {[
item[{
A :"A",
A1:"C",
A2:[]
item B[{
B1: []
}]
]}
}]
Use the delete statement, it would go something like this:
if (obj[propName] === null || obj[propName] === undefined) {
delete obj[propName];
}
You can use that inside a for loop if you have an array of items.
This is an abstract way to delete keys with null or undefined values from an object since your code isn't really that much clear.
UPDATE:
To implement similar functionality in C#, you will have to use the .RemoveKey("Key") function.
So for example say you have the key as label inside an item object, use the following syntax:
item.RemoveKey("label"); or if it was in an array use: item[index].RemoveKey("label");
Hope this helped.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have troubles understanding the meaning of (_,idx) in the following statement
arr.filter((_, idx) => idx % 2 === 0)
I understand it is filtering the array and just returning on the new array all elements that respect the condition(basically with even index). But i do not understand what this (_,idx) means?
Any help?
(_, idx) are two variable names.
arr.filter((currentValue, index, array) => )
As you can read here.
So "_" is currentValue and "idx" is the index.
"_" is not use but still defindes because it is a non-optional parameter, so you always have to give it an name.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a json web service that returns simply [].
Then, I have a javascript Object variable json_var that contains this json output. When json returns empty [], json_var is undefined. How do I check from the content of json_var that the json output is empty []?
check for the length of the response. As it was mentioned [] refers to an empty array
var test = [];
console.log(test.length); // returns 0
JSFIDDLE