Jump to content

Magic in arrays


L8V2L
 Share

Recommended Posts

i think you're referring to foreach loops http://www.w3schools.com/php/php_looping_for.asp

No... Why did you post a link to a php for loop?I'm speaking on this:
var arr = new Array(100);arr[arr.length] = "elem";arr[101]; //elemDid it iterate to the value or is it magic--in the sense that the key "101" brought the value "elem"--
Edited by L8V2L
Link to comment
Share on other sites

I believe it's because arrays start at 0 and the .length property counts from 1. So you have 100 indexes for arr.. you then add to the 101 index using the length property value( which is 101) the value "elem." From 0 to 100, the length for arr is 101.

Link to comment
Share on other sites

I believe it's because arrays start at 0 and the .length property counts from 1. So you have 100 indexes for arr.. you then add to the 101 index using the length property value( which is 101) the value "elem." From 0 to 100, the length for arr is 101.

... It's the key to getting the value I'm referring to. Not ths actually index number. It could have been
arr["foo"] = "bar";
Link to comment
Share on other sites

That's not an array, that's an object. Arrays only take numbers as indices.

/*|*|... True since arrays are objects. |*|But array number |*|index are actually string |*|index via the ECMAScript spec. And |*|also array can have name |*|properties i.g.:*/var arr;arr = new Array("0","1","2");console.log(arr.constructor === Array); //truearr['num'] = '3';console.log(arr.constructor === Array); //truearr[3]; //in most case, undefinedarr["num"]; // the string 3arr.num; // the string 3/*|*|but this is still not what I'm|*|refering to! Do arrays iterate|*|to the key/value pisition? |*|does it iterate to index 3 |*|when arr[3], or is it magic?|*|same goes for non-numeric |*|properties. i.e.: arr["num"].*/
Edited by L8V2L
Link to comment
Share on other sites

It is a little mysterious. In a simpler world, such as in c code, the array is a simple thing. In Javascript the array is not such a simple thing. I would guess that the Javascript engine maintains a set of tables to keep track of array elements.

Link to comment
Share on other sites

It is a little mysterious. In a simpler world, such as in c code, the array is a simple thing. In Javascript the array is not such a simple thing. I would guess that the Javascript engine maintains a set of tables to keep track of array elements.

You're mostly right... Wouldn't JSON be faster than? Dropping in the key and getting a return value, plus, you can set up JSON to be map as a tabular database via either arrays or objects.
Link to comment
Share on other sites

JSON isn't faster than Javascript because JSON is a data storage format. Javascript is the one doing the work of parsing JSON and turning it into Javascript objects.

 

Being able to add properties to an array is a quirk of Javascript and you shouldn't use it. If you're using it that way it's no longer an array, it's an object.

Link to comment
Share on other sites

JSON isn't faster than Javascript because JSON is a data storage format. Javascript is the one doing the work of parsing JSON and turning it into Javascript objects.

 

Being able to add properties to an array is a quirk of Javascript and you shouldn't use it. If you're using it that way it's no longer an array, it's an object.

I understand what your saying, but that last part is incorrect... at least by the test that I have giving above. Copy and paste that in the console. And you'll get the answer that is commented in my post. So going by that, Array can have name properties. It's not recommended to use it as so. But it is not illegal to do so. Array can have name properties too, and still have their constructor pointing to array.

Link to comment
Share on other sites

Yes, now what was your question again?

davej kinda answer the question. I ask what is the mechanism behind retrieving arrays elements via there keys? Does it iterate, or is it magic... please look over my other post for more detail. This is just the jus--could someone spell this word right for me-- of it.

Link to comment
Share on other sites

JSON is just the raw declaration of an object. If you had an array containing a million elements you wouldn't want to iterate through them for each array access. You would create and use an index table or some sort of hash table.

Link to comment
Share on other sites

JSON is just the raw declaration of an object. If you had an array containing a million elements you wouldn't want to iterate through them for each array access. You would create and use an index table or some sort of hash table.

Where going to go with it's just magic. So with that logical. having a containing a million elements would be easy to go through with out iterating through them with just using the key to access the element you want. So if you have an variable containing million array; which of those array in that array are named index, and they contain objects. So... So how the SQL query relational database data?

Link to comment
Share on other sites

That diagram is mostly about foreign keys.

var table;table = [];table["row0"] = [];table.row0[0] = {car:"01"};table.row0[0].car;/*i can understand your argument on querying this, but there are querying tools for querying, i.g.: JSONiq*/
Link to comment
Share on other sites

In order fo understand databases properly you have to have an understanding of the data structures they use. JSON is nothing but a textual representation of information.

 

Learn what a data structure is: http://en.wikipedia.org/wiki/Data_structure

 

Learn types of data structures used by the database:

http://en.wikipedia.org/wiki/B-tree

http://en.wikipedia.org/wiki/Hash_table

http://en.wikipedia.org/wiki/Red_black_tree

 

JSON is a really superficial thing that's unrelated to the fundamental concepts of a database.

  • Like 1
Link to comment
Share on other sites

In order fo understand databases properly you have to have an understanding of the data structures they use. JSON is nothing but a textual representation of information. Learn what a data structure is: http://en.wikipedia.org/wiki/Data_structure Learn types of data structures used by the database:http://en.wikipedia.org/wiki/B-treehttp://en.wikipedia.org/wiki/Hash_tablehttp://en.wikipedia.org/wiki/Red_black_tree JSON is a really superficial thing that's unrelated to the fundamental concepts of a database.

Thanks for the link. From what I have read, it sound a lot like xml, and json.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...