This question already has answers here:
How is a JavaScript hash map implemented?
(7 answers)
How to do associative array/hashing in JavaScript
(11 answers)
Closed 5 years ago.
My goal is to find if the value is in JavaScript array, but very efficiently. I understand that inarray? check will usually execute in O(n) where n is the length of an array because we have to traverse through every variable in the array. But hashmap will always return us the value in 0(1) (at least from my understanding)
So what if instead of traversing an array we created an key -> variable association, where the key is identical to the value and just tried getting the element by key. If it exists, then we return true, if it is 'undefined', we return false?
Has anyone ever done this before and is this even feasible in terms of javascript language?
Related
This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.
This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 1 year ago.
I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
strong text
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false
This question already has answers here:
Why is using "for...in" for array iteration a bad idea?
(28 answers)
How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop
(4 answers)
Closed 1 year ago.
This is the piece code to show my question:
var data=[];
for(var pro in data) {
console.log('Property: '+pro);
}
Array.prototype.Dummy=function() {
};
for(var pro in data) {
console.log('Property:'+pro);
}
I expect the 2 for in statements behave the same, the first prints nothing, reasonable, but the second one will list 'Dummy' only as the array's property, even Array has lots of buildin methods.
The user defined method is different from build in, why?
The reason I ask this question is that I use 3 party libraries, if I extend buildin data types(like Array, Date ect.), I'll potentially modify what their for in statements do.
This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 1 year ago.
I need to execute some code. But my code is not working properly. I have made an simple example to demonstrate the malfunction.
When I run
console.log("val" in Object.values({key:"val"})); //returns false
It gives me false. But if I run
console.log(Object.values({key:"val"}))
outputs => ['val']
I don't understand if it is supposed to work like this. If yes. Why ?
Thanks in Advance.....:)
MDN says "the in operator returns true if the specified property is in the specified object or its prototype chain.", Object.values returns an array. To check if an item exists in an array, use the Array.includes method.
console.log(
Object.values({key:"val"}).includes("val")
); // Returns true
This question already has answers here:
What "..." means in Javascript (ES6)? [duplicate]
(1 answer)
Spread Syntax vs Rest Parameter in ES2015 / ES6
(11 answers)
Closed 4 years ago.
I’m new to coding and slef teaching JavaScript.
My task I was set was to identify the largest value in an array.
My soloition works, but I needed the ‘...’ for it to work properly.
My question is, what does the ‘...’ in the last line actually mean/do?
function createAnArr(){
let myArr = prompt("Enter your numbers separated by commas:").split(",");
return myArr;
}
console.log(Math.max(...createAnArr()));
'...' it means it will be able to take any number of arguments of the respective scope
'...': The spread operator is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.
more details you can see here