This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 1 year ago.
I am aware that you can define an object reference from string like this;
obj['string']
Which basically translates to obj.string
My question is how would one extend this past the first object value? This is what I would like to pass.
obj['string1.string2']
Which fails due to that key (string1.string2) not existing within the object. I understand how this works but what I am failing to figure out is how could one get this to work and return obj.string1.string2
My use case is that I have an array of objects;
let players = [
{
name: 'Player 1',
points: {
current: 100,
total: 1000
}
},
{
name: 'Player 2',
points: {
current: 50,
total: 500
}
}
];
What I am trying to do sort the array based on the current value of points. players.points.current
The sorting method I am using is players.sort((a, b) => { return b.points.current - a.points.current });
Which works great, but in order to create a method that can take a 'sorting' string term like points.current or points.total in order to minimise code and make the function more reusable, I was trying to pass the string into the object reference which does not work for obvious reasons as mentioned before that the key does not exist. players['points.current']
Would thank anyone kindly if they could help me. Should I tackle this problem in a completely different way?
You can chain the properties, as everything inside the quote is considered a single property name string
i.e:
object['thing1']['thing2']
If you want to get this to work with a string with an unknown number of property depth, you would loop over each one, hard to explain but as an example:
let obj0 = {
obj1: {
obj2: "answer"
}
}
let string1 = "obj1.obj2"
let strArr = string1.split('.')
let objSearch = obj0;
strArr.forEach(str => {
objSearch = objSearch[str]
})
console.log(objSearch)
Keep in mind this only works on success cases where the object properties exist, you will want to have some error handling on if objSearch[str] exists etc
I recently found out that in the chrome console I was able to do something really unusual.
Here's a simple example using an array of objects.
g = [{age: 23}, {age:33}];
g.foo = "bar";
> (2) [{…}, {…}, foo: "bar"]
I think I'm able to do this because all js data structures are objects? Is there any way get access to foo:bar without knowing what it is beforehand?
g = g.filter(function(e){
return e.age !== 33;
});
> (2) [{…}]
Specifically I'm trying to filter an array, without losing the object attributes that I set like foo:"bar".
The filtered arrays I'm getting back do not contain the object attributes I had before.
So my question is what is the best way to get access to attributes that I set on an array object when I treat it like an object in this strange way?
Ideally I could run something(g) and get back {foo: 'bar'}, or a list of all the props that exist on this object that aren't related to the array aspect of it.
Or is this just a super hacky idea and I shouldn't do it like this in the first place?
Thanks!
You could use Object.keys for own enumerable keys of the object, even for arrays.
While own properties are possible, it is not advisable, because it could lead to lose some properties, if used with JSON.stringify more about disadvantage here: Is it good practice to add properties on array in javascript (still opinion-based).
var g = [{ age: 23 }, { age: 33 }];
g.foo = "bar";
console.log(Object.keys(g));
console.log(JSON.stringify(g));
This code get all enumeral keys in your object:
let g = [{age: 23}, {age:33}];
g.foo = "bar";
let keys = [];
for(var x in g){
keys.push(x);
}
console.log(keys)
But this code get all keys( from prototype also), best way for get keys -use Object.keys(g)
If you want get all keys without prototype : use Object.getOwnPropertyNames(g)
This is more of a general question than a problem I need solved. I'm just a beginner trying to understand the proper way to do things.
What I want to know is whether or not I should only use objects as prototypes (if that's the correct term to use here) or whether or not it's OK to use them to store things.
As an example, in the test project I'm working on, I wanted to store some images for use later. What I currently have is something like:
var Images = {
james: "images/james.png",
karen: "images/karen.png",
mike: "images/mike.png"
};
Because I would know the position, I figure I could also put them in an array and reference the position in the array appropriately:
var images = ["images/james.png", "images/karen.png", "images/mike.png"];
images[0];
Using the object like this works perfectly fine but I'm wondering which is the more appropriate way to do this. Is it situational? Are there any performance reasons to do one over the other? Is there a more accepted way that, as a new programmer, I should get used to?
Thanks in advance for any advice.
Introduction
Unlike PHP, JavaScript does not have associative arrays. The two main data structures in this language are the array literal ([]) and the object literal ({}). Using one or another is not really a matter of style but a matter of need, so your question is relevant.
Let's make an objective comparison...
Array > Object
An array literal (which is indirectly an object) has much more methods than an object literal. Indeed, an object literal is a direct instance of Object and has only access to Object.prototype methods. An array literal is an instance of Array and has access, not only to Array.prototype methods, but also to Object.prototype ones (this is how the prototype chain is set in JavaScript).
let arr = ['Foo', 'Bar', 'Baz'];
let obj = {foo: 'Foo', bar: 'Bar', baz: 'Baz'};
console.log(arr.constructor.name);
console.log(arr.__proto__.__proto__.constructor.name);
console.log(obj.constructor.name);
In ES6, object literals are not iterable (according to the iterable protocol). But arrays are iterable. This means that you can use a for...of loop to traverse an array literal, but it will not work if you try to do so with an object literal (unless you define a [Symbol.iterator] property).
let arr = ['Foo', 'Bar', 'Baz'];
let obj = {foo: 'Foo', bar: 'Bar', baz: 'Baz'};
// OK
for (const item of arr) {
console.log(item);
}
// TypeError
for (const item of obj) {
console.log(item);
}
If you want to make an object literal iterable, you should define the iterator yourself. You could do this using a generator.
let obj = {foo: 'Foo', bar: 'Bar', baz: 'Baz'};
obj[Symbol.iterator] = function* () {
yield obj.foo;
yield obj.bar;
yield obj.baz;
};
// OK
for (const item of obj) {
console.log(item);
}
Array < Object
An object literal is better than an array if, for some reason, you need descriptive keys. In arrays, keys are just numbers, which is not ideal when you want to create an explicit data model.
// This is meaningful
let me = {
firstname: 'Baptiste',
lastname: 'Vannesson',
nickname: 'Bada',
username: 'Badacadabra'
};
console.log('First name:', me.firstname);
console.log('Last name:', me.lastname);
// This is ambiguous
/*
let me = ['Baptiste', 'Vannesson', 'Bada', 'Badacadabra'];
console.log('First name:', me[0]);
console.log('Last name:', me[1]);
*/
An object literal is extremely polyvalent, an array is not. Object literals make it possible to create "idiomatic" classes, namespaces, modules and much more...
let obj = {
attribute: 'Foo',
method() {
return 'Bar';
},
[1 + 2]: 'Baz'
};
console.log(obj.attribute, obj.method(), obj[3]);
Array = Object
Array literals and object literals are not enemies. In fact, they are good friends if you use them together. The JSON format makes intensive use of this powerful friendship:
let people = [
{
"firstname": "Foo",
"lastname": "Bar",
"nicknames": ["foobar", "barfoo"]
},
{
"firstName": "Baz",
"lastname": "Quux",
"nicknames": ["bazquux", "quuxbaz"]
}
];
console.log(people[0].firstname);
console.log(people[0].lastname);
console.log(people[1].nicknames[0]);
In JavaScript, there is a hybrid data structure called array-like object that is extensively used, even though you are not necessarily aware of that. For instance, the good old arguments object within a function is an array-like object. DOM methods like getElementsByClassName() return array-like objects too. As you may imagine, an array-like object is basically a special object literal that behaves like an array literal:
let arrayLikeObject = {
0: 'Foo',
1: 'Bar',
2: 'Baz',
length: 3
};
// At this level we see no difference...
for (let i = 0; i < arrayLikeObject.length; i++) {
console.log(arrayLikeObject[i]);
}
Conclusion
Array literals and object literals have their own strengths and weaknesses, but with all the information provided here, I think you can now make the right decision.
Finally, I suggest you to try the new data structures introduced by ES6: Map, Set, WeakMap, WeakSet. They offer lots of cool features, but detailing them here would bring us too far...
Actually, the way you declared things brings up the "difference between associative arrays and arrays".
An associative array, in JS, is really similar to an object (because it's one):
When you write var a = {x:0, y:1, z:3} you can access x using a.x(object) or a["x"](associative array).
On the other hand, regular arrays can be perceived as associative arrays that use unsigned integers as ID for their indexes.
Therefore, to answer your question, which one should we pick ?
It depends : I would use object whenever I need to put names/labels on thing (typically not for a collection of variables for instance). If the type of the things you want to store is homogeneous you will probably use an array (but you can still go for an object if you really want to), if some of/all your things have a different type than you should go for an object (but in theory you could still go for an array).
Let's see this :
var a = {
x:0,
y:0,
z:0
}
Both x,y,z have a different meaning (components of a point) therefore an object is better (in terms of semantic) to implement a point.
Because var a = [0,0,0] is less meaningful than an object, we will not go for an array in this situation.
var storage = {
one:"someurl",
two:"someurl2",
three:"someurl3",
}
Is correct but we don't need an explicit name for every item, therefore we might choose var storage = ["someurl","someurl2","someurl3"]
Last but not least, the "difficult" choice :
var images = {
cathy: "img/cathy",
bob: "img/bob",
randompelo: "img/randompelo"
}
and
var images = ["img/cathy","img/bob","img/randompelo"]
are correct but the choice is hard. Therefore the question to ask is : "Do we need a meaningful ID ?".
Let's say we work with a database, a meaningful id would be better to avoid dozens of loops each time you wanna do something, on the other hand if it's just a list without any importance (index is not important, ex: create an image for each element of array) maybe we could try and go for an array.
The question to ask when you hesitate between array and object is : Are keys/IDs important in terms of meaning ?
If they are then go for an object, if they're not go for an array.
You're correct that it would be situational, but in general its not a good idea to limit your program by only allowing a finite set of supported options like:
var Images = {
james: "images/james.png",
karen: "images/karen.png",
mike: "images/mike.png"
};
Unless, of course, you happen to know that these will be the only cases which are possible - and you actively do not want to support other cases.
Assuming that you dont want to limit the possibilities, then your array approach would be just fine - although personally I might go with an array of objects with identifiers, so that you arent forced to track the index elsewhere.
Something like:
var userProfiles = [
{"username": "james", "image": "images/james.png"},
{"username": "karen", "image": "images/karen.png"},
{"username": "mike", "image": "images/mike.png"}
];
If you have an array of objects like
[{rID:53, name:Roger, age:43},{rID:12, name:Phil, age:22}]
is it possible instead to give each a object a key like
[53:{name:Roger, age:43},12:{name:Phil, age:22}]
?
I understand that each object has an index number, but I'm looking for a way to find objects not based on their index pos, and preferably without having to loop through until you find an object with rID=53 type thing.
I'd be using PKs from mysql rows to give each object it's key.
Cheers
If you want an unordered collection of variables with arbitrary keys. Then use an object.
{"53":{name:Roger, age:43},"12":{name:Phil, age:22}}
Arrays can have only numeric keys, but if you want to assign a value to an arbitrary position, you have to do so after creating the array.
var some_array = [];
some_array[53] = {name:Roger, age:43};
some_array[22] = {name:Phil, age:22};
Note that this will set the array length to 54.
Note that in both cases, you can't have multiple values for a given property (although you could have an array of values there).
You need an object for that.
{
53: {
name: "Roger",
age: 43
},
12: {
name: "Phil",
age: 22
}
}
No. You cannot have a key and an index in a js array. Arrays only have indices, Objects only have keys. There is no such thing like a associative array in JavaScript. If the order (of your original array) does not matter, you can use an object though:
{53:{name:"Roger", age:43},12:{name:"Phil", age:22}}
You can use objects to do this
var map = {"53":{"name": "Roger", "age": "43"},"12":{"name": "Phil", "age": "22"}};
Then access the value like an array (note, i'm using string because that's what keys are stored as, strings, you can access it using an integer, but it will just be converted to a string when searching for the property anyway):
map["53"]
You can also loop through this map object:
for (var key in map) {
var value = map[key];
}
I have a JSON object that looks something like this (result from an AJAX call):
json{
code: 0,
resultVal: Object {
data:
[
Object{
generatedName: name1,
generatedValue: value1
},
Object{
generatedName1: name2,
generatedValue1: value2
}....
],
anotherItem: true,
...
}
}
To clarify resultVal is an object and data is an array of objects, and each object in that array will have two values who's names I will not know in advance.
I am having a problem because I need generatedName and generatedValue to be GenerateName and GeneratedValue. These names and values are usually not the as each other. I know can access each Object through json.resultVal.data[#], but that's as far as I have gotten. json.resultVal.data[0].name returns undefined.
Once I can get those values isolated I can make the fixes I need.
NOTE I am running these calls through Chrome's debugger. The thinking is once I am able to isolate the value I can write the code to fix it using that call. It takes some time to get to this point in the application.
Any suggestions?
If I understood it right, you need to iterate over all keys for all objects in "json.resultVal.data". Try using a for/in loop to iterate over the "data" object, as in:
for( var i in json.resultVal.data ) {
for( var k in json.resultVal.data[i] ) {
/* here "k" will be key string ("generatedName", "generatedValue", ...) */
}
}