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.
Related
This question already has answers here:
adding custom functions into Array.prototype
(7 answers)
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Closed 2 years ago.
In some code I'm working on I created a JavaScript function that is applied solely to Arrays, and I thought I'd try adding it as a member function.
I added it like so:
Array.prototype.myfunc = function(a){
...
}
Which works fine for the most part. The problem I run into then is with a for-in loop. It includes that function in the loop. If I then type in this snippet:
var bar, foo = ['alpha', 'bravo', 'charlie'];
for(bar in foo) console.log(foo[bar]);
Then the output is along the lines of:
alpha
bravo
charlie
function myFunc(a){
...
}
So is there any way of doing this but avoiding it showing in the for-in loop?
You can use Object.defineProperty to create non-enumerable attributes of any object, including arrays. Remember in JavaScript, arrays are just objects with numbers for keys, which is why attributes you add to them come up in a forEach.
This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.
This question already has answers here:
Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?
(3 answers)
Closed 8 years ago.
Both of these obviously do similar things but my question is why is one on the prototype and one on the Object?
For example, both of these called differently. Is there a logical reason why this is the case?
var o = {name: "value"}
o.hasOwnProperty("name") //true
Object.getOwnPropertyNames(o); //name
//Couldn't the above have been coded so we can run o.getOwnPropertNames();
Thanks.
The problem with adding these methods to the prototype is taht they are prone to getting overloaded.
var o = {};
o.getOwnPropertyNames = 17;
//What now?
In fact, you would often see people doing
Object.prototype.hasOwnProperty.call(o, propname);
in order to avoid the overloading issue with hasOwnProperty.
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]);
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