This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I have a following object:
var sample =
{
count: {
'20-01-2015/17': {
'a': 553056,
'b': 622123
},
'20-01-2015/18': {
'a': 519008,
'b': 610474
}
}
}
I want to find all the keys for count property. Also, all the keys for 20-01-2015/17 property also.
var times = Object.keys(sample.count);
console.log(times);
var time = '20-01-2015/17'
//This works
var props = Object.keys(sample.count[time]);
console.log(props);
//But this doesn't work. I am not able to understand that.
props = Object.keys(sample.count.time);
console(props)
I didn't understand why the first approach is working, but not the second one.
Object.keys() is used to ENUMERATE all properties that have their Enumerable flag turned on in the object descriptor including functions, for example, if you do this Objects.keys(Array.prototype) it will print all the functions defined on the prototype property of the Array which is shared by all Array objects.
Related
This question already has answers here:
Why were ES5 Object methods not added to Object.prototype?
(2 answers)
Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?
(3 answers)
Closed 3 years ago.
There are methods like Object.values fo Object.keys, but why aren't these methods inside the object's prototype? Is there a good reason for this?
Example:
const user = { name: 'John', role: 'admin' };
const keys = user.keys() // instead of Object.keys(user);
const values = user.values() // instead of Object.values(user);
Because everything is an object in JavaScript. If you add a method to the Object's prototype, it would be inherited to everything, it can't (or shouldn't, as it then hides the original method) be used as a name of a custom method. That means that if the Object.prototype would get polluted with a lot of methods, it would make the choice of property names more difficult:
1..keys() // Did you expect this to work?
This question already has answers here:
Can I assume the order of response from Object.keys same?
(2 answers)
Closed 4 years ago.
Consider the following code:
const obj = { 'a': 1, 'b': 2, 'c': 3 };
const keys1 = Object.keys(obj);
const keys2 = Object.keys(obj);
Am I guaranteed that the elements of keys1 will be in the same order as those in keys2?
Will Object.keys() always return keys in the same order if called on an object which remains unmodified between calls (assuming both calls are performed in the same environment)?
My question is similar to the question asked here about dictionary behavior in Python.
Yes, they will be in the same order if written in the format as you have mentioned.
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 8 years ago.
I am trying to initialize an object and and assign a property of its own to one of the properties.
But my syntax is incorrect.i am referring to the following line:
PCMACUrl = genericURL + "/test"
i have tried
testList[0] = {
executionTimeSec:60,
genericURL:"www.gmail.com",
comments: "",
PCMACUrl = genericURL + "/test"
};
After re-reading all together I believe this is what you are looking for:
(added after init, yes, I know, but it's clean and simple :)
var data = {
'PropA': 1,
'PropB': 2,
'PropC': 3
};
data.PropD = data.PropC+5;
console.log(data); //Object {PropA: 1, PropB: 2, PropC: 3, PropD: 8}
Or, another way to look at it:
if it is possible use backend to generate the object you would like
probably you could also just get rid of same data in same object and use it differently when you call it (referencing to first object propertie and adding second on-the-go:
.../ = data.PropC+anotherData.PropA /...
This question already has answers here:
How to duplicate object properties in another object? [duplicate]
(16 answers)
Closed 8 years ago.
Given this object:
var myObject = {name: 'David', date: '11/13/2014'};
I want my constructor to set its object properties based on myObject:
function myClass(_object){
// set given object's properties here
}
I want to set the properties through a loop, so I wont have to set every property by hand - because the object will be dynamic.
I'm looking for a cross-browser solution.
Perhaps what you want is this:
function MyClass(_object) {
for (var p in _object) {
if (_object.hasOwnProperty(p)) {
this[p] = _object[p];
}
}
}
Don't forget to use new:
var obj = new MyClass(myObject);
Fiddle
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript arrays braces vs brackets
I have a simple question that I can't find an answer to with Google.
What is the difference between
var foo = { };
and
var bar = [ ];
An example would help.
foo = {} is not an array, but an object (created using object literals, {}).
bar = [] is an array, which inherit various methods from the Array constructor. An array also has various properties, such as .length.
EDIT (regarding comment):
The a property of an object can be accessed through foo.a or foo["a"].
Looping through the properties of the object should be done using a for( .. in .. ) loop.
var foo = {"a":true, "b":false};
for(var prop_name in foo){
alert(prop_name + " - " + foo[prop_name]);
}
This code will show two alert messages:
a - true
b - false