Is there a shorter way to define a list, for example. 100 new arrays?

My goal is to compare the first names with the name of the img element and insert the last name when the first name matches the title of the img element. But before programming it, I would like to ask you a question.

Is there an easier way to create a new list of arrays containing the first and last name than I chose? It would be very inconvenient if I had about 100 new arrays, each of which would contain the name and surname.

 var username = new Array();

  username[0] = new Array();
  username[0]["first name"] = "daniel";
  username[0]["last name"] = "obrian";

  username[1] = new Array();
  username[1]["first name"] = "stuart";
  username[1]["lastname"] = "oconner";

Thanks in advance.

+3
source share
3 answers

You are looking for object literal syntax:

var username = new Array();
username[0] = { "first name": "daniel", "last name": "obrian" };
username[1] = { "first name": "stuart", "last name": "oconner" };

, , , . JavaScript , .

, :

var username = [
    { "first name": "daniel", "last name": "obrian" },
    { "first name": "stuart", "last name": "oconner" }/*,
    { ... },
    { ... },
    { ... } */
];
+7

:

var username = [
    { first_name: "daniel", last_name: "obrian"},
    { first_name: "stuart", last_name: "oconner"},
    // etc...
];

first_name "first name" , .

+2

Do you mean something like this?

var people= [["daniel", "obrian"],["stuart","oconner"]];
alert(people[0][0]);
0
source

Source: https://habr.com/ru/post/1771298/


All Articles