0

I need some help with this because it is the first time I will have much content in a non-CMS Website which needs to be sorted, setted up and appended to various divs.

I have around 250 people with each Forname, Surname, Telephone number, E-Mail adress, and some infostring.

I thought about doing this with an array like

var peopleArr = ["John, Doe, 0123456789, johndoe@gmail.com, infoshere",
               "Ellen, Page, 0987654321, ellenpage@gmail.com, anotherinfo",
               "Megan, Fox, 0249235331, meganfox@gmail.com, niceinfo",
               "and so on"]

but I really don't know if this make sense, since I have to sort all array elements by surname and after that I need to put each element in an own div one the site.

Would you say this makes sense or would you suggest something different? To mention is, that I don't have any experience with XML or JSON, but I am open for it if it makes the most sense.

Thanks in advance.

supersize
  • 13,764
  • 18
  • 74
  • 133
  • 1
    I would rather define a "people" object, with each data as a property, and place each individual object in an array. Then, you can define a custom sort for your array following [this answer](http://stackoverflow.com/questions/9316119/sort-complex-array-of-arrays-by-value-within). – Laurent S. Apr 10 '14 at 09:50
  • @Bartdude could you post this as answer, I can't get it how exactly you mean! Cheers – supersize Apr 10 '14 at 09:55
  • Php has array sorting functions which you can use http://www.php.net/manual/en/array.sorting.php – Scott Apr 10 '14 at 09:58
  • 1
    @Scott : so has also any DBMS , but there's nothing here about server-side code... – Laurent S. Apr 10 '14 at 10:00
  • @bartdude, oops, though this was php I shall edit... – Scott Apr 10 '14 at 10:03

1 Answers1

1

You could try something like this (as also Bartdude suggested):

var peopleArr = [
                    { forname: 'John', surname: 'Doe', phone: '0123456789', email: 'johndoe@gmail.com', info: 'infoshere' },
                    { forname: 'Ellen', surname: 'Page', phone: '0987654321', email: 'ellenpage@gmail.com', info: 'anotherinfo' },
                    { forname: 'Megan', surname: 'Fox', phone: '0249235331', email: 'meganfox@gmail.com', info: 'and so on' }
                ];
Community
  • 1
  • 1
Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30
  • are the variables declared by its own inside the array? – supersize Apr 10 '14 at 09:57
  • Wouldn't have said better :-) And Yes, supersize, this is a declaration/initialization at the same time, putting the square braquets indicate an array and the curly ones indicate an object. The good thing is that you should have a lot of work to do on front-end if at some point you put all this data in a database and fetch it from the server, cause that's how they would be returned if you use JSON – Laurent S. Apr 10 '14 at 10:00
  • @Bartdude so but then how can I get for example the forname of index 2? `peopleArr[2].??` – supersize Apr 10 '14 at 10:01
  • `peopleArr[2].forname` will get you the data – Laurent S. Apr 10 '14 at 10:03