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 last month.
Improve this question
Im working with javascript but maybe there is also an general answer.
Assuming we have 10 objects with ids.
Would it be faster to search for an object with an specific id by using the array find() methode:
const found = entries.find(entries => entries.id === myId)
or initial collect the data in a Map and use the get() methode?
Thanks to the comments I know now:
Array.find() is similar iterating/looping through an array and that means, it is slower then using get(), which is not a loop.
So if I want to search only one time in this array, find() should be enough. If I want to find multiple elements in that array. It makes sense to generate a Map from that array.
PS: The amount of 10 was an example. I just wanted to know what generally is faster.
Related
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 7 months ago.
Improve this question
I'm trying to learn javascript by following a tutorial on youtube, and I found this segment of code.
I understand what filter generally does or at least its purpose, but I don't know anything about filter conditions what they do exactly? Any explanation will be appreciated.
setFilteredProducts(
products.filter((item) =>
Object.entries(filters).every(([key, value]) =>
item[key].includes(value)
)
)
);
.entries(filters)
basically returns an array iterator object which is then being iterated using
.every(([key, value])
This is basically creating an iterator(entries) and iterating(every) through it.
Finally, .includes(value) checks if the value is present in item[key]. It is basically a function to search for an element.
Hope it helps!
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
I have an array of objects in Javascript (I use Vue.js2 in my project) and with .splice I can easily delete all the elements inside of it. The problem is, when only one element remains, I can't delete it because it stays there no matter what.
The easiest way to clean up an array is to make it [array].length = 0
You can use .length, .filter, .pop and im pretty sure you can .splice the last element, maybe there is something else u havent noticed yet.
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 3 years ago.
Improve this question
I'm new to JavaScript and I got this awkward doubt when I went through the concepts of objects in JavaScript, since the disadvantage of arrays in most of the programming languages is that it can hold only homogeneous datatypes. But that is not the case in JS. So why not we use arrays instead of objects in JS?
Array should be used when you have to access values a specific order or want looping through , if you don't care about the key name go with array or if you are concerned with value should be accessed with string key then objects are best .
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 4 years ago.
Improve this question
I am stuck with some of concepts in forEach. Referred some of sites to clear my thought about why return inside forEach always returns undefined where map, find statements returns the values but not getting correct idea. anyone please redirect me to correct websites if there or please clear my doubt.
Thanks!
so basically when you use forEach you say that you want to a particular thing for every element, note that to accomplish that task you dont need to return anything and even if you do, just think what use will it be ?
but when you map you want every element to be converted to something, now this something has to be the return value of the function inside map
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 8 years ago.
Improve this question
How can I go through a certain ul-Element and search every li-Element in it? I've only found jquery solutions, but don't want to use it here.
Looping through a selection without some external library such as jQuery or D3 is kind of annoying because the selection will not return an Array but instead an HTMLCollection or NodeList (depending on your browser). I recommend using some third party library if you are doing this sort of thing often. Anyway here's how to do it in pure JavaScript.
// This is not an Array but either an HTMLCollection or a NodeList
var selection = document.getElementById(<id>).getElementsByTagName("li");
for(var i = 0; i < selection.length; i++)
{
// do something with selection[i]
}
It is important to note that because your selection is not an Array you cannot use for-each loops (the intended way at least) or any higher level Array functions as described here.