This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 9 years ago.
I am aware that everything is an object in JavaScript but something struck me when i was using the console of Internet Explorer -F12 (Yes IE, not allowed to use other browsers)
If i type a sample array in the console as:
[1,2]
the output is
1,2{
0:1,
1:2
}
Does this mean that JavaScript converts Array into an Object with keys and values?
Yes, arrays in JS are simply objects with numeric keys. You could do the reverse:
var myarray = { 0: 'first', 1: 'second', 2: 'third' };
console.log(myarray[1]);
Related
This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);
This question already has answers here:
adjust console.log behaviour of custom object
(3 answers)
How to change string representation of objects in Nodejs debug console view
(5 answers)
Closed 3 years ago.
I was hoping I could override what is displayed by console.log. I was assuming the below:
const arr = ['ben', 'john', 'amy'];
Array.prototype.toString = function(){return 'alex';}
console.log(arr); // hoping this could result in 'alex' being printed to the screen. Prints the array.
I was hoping I could make the array print my 'alex' override to the screen but it looks like that is not how console.log works. The toString function must not be was is used to determine the log. Does anyone know how this is determined or if it can be overridden?
This question already has answers here:
How to loop through a plain JavaScript object with the objects as members
(28 answers)
Closed 6 years ago.
In the following JavaScript code,
obj = {};
// This work as intented
obj['a'] = { item1: 'a1', item2: 'a2' };
console.log(obj);
// Object.keys() works too
console.log(Object.keys(obj));
// forEach does not, why? and how to fix?
console.log('forEach');
obj.forEach(o => console.log(o));
What is needed to have forEach working?
What you have here is a JavaScript question, not a TypeScript question. TS and JS have the same runtime semantics.
forEach is a method of Array. Objects don't have forEach. The semantics of forEach don't make sense on regular objects -- your obj doesn't have a length or a 0 property, for example, which are the kinds of things forEach looks for.
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 7 years ago.
why is that? I assumed it's some implicit type conversion at first, but [] == [] is also false.
Arrays in javascript are Objects. Objects are compared by identity. So no two objects created by different literals (or by other means) are going to be equal (either strictly (===) or loosely (==)).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Are Javascript arrays sparse?
Is the following "safe" in JavaScript? (as in, can be expected to work reliably on all JavaScript engines)
a = [];
a[100] = "hello";
a[100] == "hello"; // should be true
Yes. Arrays in JavaScript are sparse and your code is expected to work in all JavaScript implementations.
You can get into requirements in the section 15.4 of the specification(PDF).
Short summary: array is special object that have length property adjusted when one adds elements at properties with numeric names (like `a[123]="test"). Other methods like join take length into account duuring operations.
Yes, why wouldn't it work? Its perfectly acceptable syntax.
You can even assume
a[100] === "hello"; // will return true