Reputation: 2084
I have an object like this :
{
" Fairfield, D. H." : [0, 0, 3, 3, 2, 2],
" Mish, W. H." : [3, 0, 3, 3, 2, 2],
" Baker, D. N." : [0, 0, 0, 0, 2, 2],
" Curtis, S. A." : [0, 0, 3, 0, 2, 2],
"Ocuna, M. H." : [0, 0, 0, 0, 0, 0],
" Ogilvie, K. W." : [0, 0, 0, 0, 2, 0]
}
so in my object (for example the first line) the key
is " Fairfield, D. H."
and the value
is [0, 0, 3, 3, 2, 2]
to call the third line in my code I have to use its key as the following :
console.log(myObj[" Baker, D. N."]);
but I don't want to call that line by its key, I want to call it by it's index for example :
console.log(myObj[2]);
and then get the key for that line in my example is " Baker, D. N."
.
How can I do that ?
I can't work with an array instead because I'm getting that object by a json
call so I have to work on it as it is.
What that object means is that the keys are authors names and the value is how many relations that other has with the other authors.
for example the author " Fairfield, D. H."
has 0
relation with himself, 0
relation with " Mish, W. H."
, 3
realtions with " Baker, D. N."
.....
what I want to do is to create an array with the names of authors and another array with the relations between these authors so in the end it has to look something like this :
nodes = [
" Fairfield, D. H.",
" Mish, W. H.",
" Baker, D. N.",
" Curtis, S. A.",
"Ocuna, M. H.",
" Ogilvie, K. W."
]
edges = [
["Fairfield, D. H.", " Baker, D. N.", 3],
["Fairfield, D. H.", " Curtis, S. A.", 3],
["Fairfield, D. H.", "Ocuna, M. H.", 2],
["Fairfield, D. H.", " Ogilvie, K. W.", 2],
[" Mish, W. H.", "Fairfield, D. H.", 3],
[" Mish, W. H.", " Baker, D. N.", 3],
[" Mish, W. H.", " Curtis, S. A.", 3],
[" Mish, W. H.", "Ocuna, M. H.", 2],
.........
]
In my code I have something like this :
$http.get('data/graphAuteur.JSON').then(function(response) {
var nodes = [];
var edges = [];
angular.forEach(response.data, function(authorRelations, authorName) {
nodes.push(authorName.trim());
angular.forEach(authorRelations, function(relation, relationIndex) {
if (relation != 0) {
edges.push([authorName.trim(),relationIndex,relation]);
}
});
});
console.log(edges);
}
in the console this looks like :
edges = [
["Fairfield, D. H.", 2, 3],
["Fairfield, D. H.", 3, 3],
["Fairfield, D. H.", 4, 2],
["Fairfield, D. H.", 5, 2],
[" Mish, W. H.", 0, 3],
[" Mish, W. H.", 2, 3],
[" Mish, W. H.", 3, 3],
[" Mish, W. H.",4, 2],
.........
]
so what I need is to change the relationIndex
in the line edges.push([authorName.trim(),relationIndex,relation]);
to something like this response.data[relationIndex][0]
so for example if the relationIndex
was 2
the response.data[relationIndex][0]
or whatever should return the string " Baker, D. N."
Upvotes: 2
Views: 65
Reputation: 19352
You can extract the list of authors with this code:
var data = {
" Fairfield, D. H." : [0, 0, 3, 3, 2, 2],
" Mish, W. H." : [3, 0, 3, 3, 2, 2],
" Baker, D. N." : [0, 0, 0, 0, 2, 2],
" Curtis, S. A." : [0, 0, 3, 0, 2, 2],
"Ocuna, M. H." : [0, 0, 0, 0, 0, 0],
" Ogilvie, K. W." : [0, 0, 0, 0, 2, 0]
};
var authors = [];
for (var name in o) authors.push(name);
Now you have in authors
:
[" Fairfield, D. H.", " Mish, W. H.", " Baker, D. N.",
" Curtis, S. A.", "Ocuna, M. H.", " Ogilvie, K. W."]
However, as bhspencer wrote, there is no guarantee that the order will be the same that you expected, so your numbers might not fit where you expect them.
It would be best to include the (ordered) list of authors in the json data.
Upvotes: 0
Reputation: 13570
In javascript object properties do not have guaranteed order. Which is to say there is no property of your object that has position two.
Does JavaScript Guarantee Object Property Order?
If you need ordered items you need to put them in an array.
Upvotes: 2