Empty array is shown as object [duplicate] - javascript

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

Related

Is there a way to log a JS object except for one key in it? [duplicate]

This question already has answers here:
How can I clone a JavaScript object except for one key?
(25 answers)
Closed 6 months ago.
I have a JS object that i want to log to the console but one of its values is 5 million characters of a hashed image so im not really interessted in that. Is there a way to exclude this key from the console log / is there a short form for cloning the object, deleting the value and logging it?
Not a duplicate of "cloning object except for one key" btw
One very easy way to do this is by using the spread operator [MDN Docs]
const yourObject = {id:"something",img: "5M-character-stuff"}
console.log({...yourObject, img: "short-img"});
// logs: {id:"something",img: "short-img"}

How to use filter to compare to arrays to find missing object? [duplicate]

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.

"in" keyword not working properly when used with object.value in node [duplicate]

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

How to merge two arrays at run time [duplicate]

This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.

Use variable to access array results in undefined [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 5 years ago.
I am getting several arrays returned from a GET reponse, which I need to access. Since their names can change based on the request, I have a variable specifing which array to use.
However I always get undefined. See this:
console.log(current); // trips_out_201702
console.log(theResponse.current); // undefined
console.log(theResponse.trips_out_201702); // this works
How can I make theResponse.current work such that it returns what current actually stands for? Why do I get undefined there?
When the property key in an object is a variable you can use the square bracket notation to access that property.
console.log(theResponse[current]);
when acessing with dynamic attribute You should do as
theResponse[current] not theResponse.current
You are trying to get array value using object's way.
You should try this one instant variable['keyName']
Good Luck!

Categories