multiple objects with the same key - javascript

I have an array of objects:
[object, object, object, object, object]
that i want to set a key to each using the object title:
{test:object, tester:object, foo:object, bar:object, test:object}
so i could say array.test instead of having to do array[0].title.
However the key can be generic, and when there are multiple objects with the same key it replaces the original.
Is what i want to do impossible without adding an index value to the key? In which case is the tidiest solution what i originally had array[0].title.

is there any way to set the same key to multiple objects without
replacing it other than appending an index value to to the key?
No. The object keys are unique, meaning a key can hold only one value. If you like to hold multiple value, then you assign array value to it. like { key : [value1, value2 ...]} but this is of no use for your problem. Also you are not sure what is the key value, and that is the whole objective of the object/map.
is the way im doing it the best way?
Yes. For the problem you mentioned using array data-structure/construct is good.

Related

javascript adding a property to all objects in an array

I have an array of objects that is fed by an external API e.g
[{prop: val1}, {prop: val2}, {prop: val3}....]
I have to feed this object(my code) to a third-party library which expects the name of the property on the object to be 'xyz' instead of 'prop'.
What is the most efficient way (memory wise and faster) basically avoiding both:
1. iterating over the array
2. adding 'xyz' property to all objects in the array
to achieve this?
I am thinking along the lines of adding a getter for xyz to all objects that return the 'prop' value, but that does not save the looping.
Adding the getting on the prototype level (Object.property) seems like a bad idea at this point.
Edit: I am not looking for different ways to loop through arrays in javascript like forEach or map. I have a very specific ask, and i am interested in exploring if it is at all possible to simply have a property proxy for 'xyz'.
Array map is used to cycle trough an array.
myArray.map(function(obj){
obj.xyz = 'yourvalue';
return obj;
}
You can use Array.map to create a new from the array you received by the API.
var newArray = oldArray.map(function(obj){
return {newKey : obj.prop};
});
In this example, newKey will be the key property you want, instead of 'prop', and it's assigned the old 'prop' value

count the number of JSON objects in object using javascript or jquery

i have a JSON data from AJAX response as below
{
"2015001":{"sname":"name1","01-07-2015":null,"02-07-2015":"0","03-07-2015":"0","04-07-2015":"0","05-07-2015":null,"06-07-2015":"0","07-07-2015":"0","08-07-2015":"0","09-07-2015":"0","10-07-2015":"0","11-07-2015":null,"12-07-2015":null,"13-07-2015":"0","14-07-2015":"1","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"0","22-07-2015":"0","23-07-2015":"0","24-07-2015":"1","25-07-2015":"1","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
"2015002":{"sname":"name2","01-07-2015":null,"02-07-2015":"0","03-07-2015":"1","04-07-2015":"1","05-07-2015":null,"06-07-2015":"0","07-07-2015":"0","08-07-2015":"0","09-07-2015":"0","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"1","14-07-2015":"1","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"0","22-07-2015":"0","23-07-2015":"0","24-07-2015":"0","25-07-2015":"0","26-07-2015":null,"27-07-2015":"0","28-07-2015":null,"29-07-2015":"0","30-07-2015":"0","31-07-2015":"0"},
"2015003":{"sname":"name3","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"0","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"0","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"2","22-07-2015":"0","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
"2015004":{"sname":"name4","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"0","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"2","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"0","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"0","30-07-2015":"2","31-07-2015":"2"},
"2015005":{"sname":"name5","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"0","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"2","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
"2015006":{"sname":"name6","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"0","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"}
}
each object has same number of objects. Here i want to count the number of objects in the first object(2015001). The key will change upon every request.
i tried
console.log(Object.keys(data[2015001]).length)
and i got what i need
but how can i do without key(2015001)
If you always want to know the number of keys of the first object, then you should use Object.keys(data[Object.keys(data)[0]]).length
Note that if the order is important to you, you should send the keys inside an array and not inside an object, as according to the specification, an object is an unordered set of name/value pairs.
You could get the first key and use it to access the object, probably the fastest way, but then again your information is a bit unclear, you are talking about objects and more objects but I am unsure when you talk about the outer object or when about the inner object.
for (var k in Object) {
break
}
console.log(Object.keys(data[k]).length)

How do I access the second item in this Javascript object?

This is probably really simple but I have a statement...
console.log($scope.data);
And in the console window I see...
[pension: "123", postcode: "GL"]
I have two questions...
What type of object is this?
How do I get access to the value (rather than the key) with a for
loop?
I tried...
for (item in $scope.data) {
console.log(item[1]);
}
But all I get is...
e
o
Not I cannot use the 'pension' or 'postcode' I need to be able to iterate the collection.
I was thinking I would need to use JSON.parse? But this seems like an overkill, because I originally instantiated $scope.data as an array. So why cant I access it like an array?
That's just how for..in loops work. The keys are assigned to item one by one, and then you use $scope.data[item] to get the value of that particular key.

Create an associative array with integer keys

I need to create an associative array in javascript with an integer key as follows;
a["10"] = "ten";
but when i create an array, it puts the value to the 10th index of the array and it creates an array with the length 11. I want it to be a key value pair. I know this can be done by using objects but i need an array only.
JavaScript does not have associative arrays. The only way to do this in JavaScript is to use objects:
var a = {
'10': 'ten'
};
ECMAScript does have Associated Arrays1 - Objects (and by extension, Arrays) are an example
However, some properties of Arrays are treated specially:
Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P ..
.. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index ..
Thus, given arr = [], the expressions arr["1"] and arr[1] refer to the same property name. Since P (the property name) is "1" and length is 0 from above, then assignment to such property will set arr.length to ToUint32(P)+1, or 2.
It is not possible to change this behavior. If you wish to not have a special length property, then use a "normal" Object instead of an Array. However, many of the Array.prototype functions can be used with arbitrary objects (with some implementation quirks aside) that have a length property and an Object can be created such that it uses Array.prototype as its own prototype.
All that being said, the post does not say what the real issue is. Instead of supposing that it must be done in that particular manner, consider explaining what the intent is: e.g. why a["10"]? And what is wrong if there are "11 items" if the object will be used in a List?
1 Please read the article before debating this statement: the term "Array" in the name does not imply an ordered sequence nor does it preclude an additional notion of a Length or the use of Hashing, etc. If you are going by a different definition, make sure to specify what it is and what the desired behavior is for a given operation.

How do I access the first key of an ‘associative’ array in JavaScript?

I have a js 'associative' array, with
array['serial_number'] = 'value'
serial_number and value are strings.
e.g. array['20910930923'] = '20101102'
I sorted it by value, works fine.
Let's say I get back the object 'sorted';
Now I want to access the first KEY of the 'sorted' array.
How do I do it? I can't think I need an iteration with
for (var i in sorted)
and just stop after ther first one...
thanks
edit: just to clarify, I know that js does not support associative arrays (that's why I put it in high commas in the Title).
2021 Update
Since ES6, properties with string keys are enumerated in insertion order. Here's a nice summary. My original answer from 2010 was correct at the time and is preserved below:
Original answer
JavaScript object properties are specified to have no order, much though many people wish it were different. If you need ordering, abandon any attempt to use an object and use an Array instead, either to store name-value objects:
var nameValues = [
{name: '20910930923', value: '20101102'},
{name: 'foo', value: 'bar'}
];
... or as an ordered list of property names to use with your existing object:
var obj = {
'20910930923': '20101102',
'foo': 'bar'
};
var orderedPropertyNames = ['20910930923', 'foo'];
Try this:
// Some assoc list
var offers = {'x':{..some object...}, 'jjj':{...some other object ...}};
// First element (see attribution below)
return offers[Object.keys(offers)[0]];
// Last element (thanks to discussion on finding last element in associative array :)
return offers[Object.keys(offers)[Object.keys(offers).length - 1]];
Actually JavaScript doesn't support associative arrays, so you can't loop through it in an implied order (e.g. you can't access it via the indexer property array[0] won't access the first element in your object). The syntax is what makes it look like it does, but in reality it doesn't. So you have no "Order" to your objects.
http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
Javascript does not have, and does not
support Associative Arrays. However…
All arrays in Javascript are objects
and Javascript's object syntax gives a
basic emulation of an associative
Array. For this reason the example
code above will actually work. Be
warned that this is not a real array
and it has real pitfals if you try to
use it. The 'person' element in the
example becomes part of the Array
object's properties and methods, just
like .length, .sort(), .splice(), and
all the other built-in properties and
methods.
Just thinking off the top of my head, but could you have another array with the key value pairs swapped?
So the answer would be arrayKeyValueReversed['20101102'] = '20910930923';
When you sort the array, use the first item (array[0]) as the key to get the value in the arrayKeyValueReversed.

Categories