每次单击都会向关联数组添加新元素

时间:2022-08-26 14:07:29

This is probably very easy, but I just can't figure out how to solve it right now. Each time a submit button is clicked, the function below checks input field 1 (name), and if not empty adds the value to an associative array, and continue with the description.

这可能很容易,但我现在无法弄清楚如何解决它。每次单击提交按钮时,下面的函数都会检查输入字段1(名称),如果不是空,则将值添加到关联数组,然后继续描述。

What I want is to add a new level 1 element to the array every click, that should hold these value, so that it after three clicks looks like this:

我想要的是每次点击都要向数组添加一个新的1级元素,它应该保存这些值,以便三次点击后如下所示:

Click 1:
    listObject[0]['listObjectName'] = 'Name 1';
    listObject[0]['listObjectDesc'] = 'Desc 1';

Click 2:
    listObject[1]['listObjectName'] = 'Name 2';
    listObject[1]['listObjectDesc'] = 'Desc 2';

Click 3:
    listObject[2]['listObjectName'] = 'Name 3';
    listObject[2]['listObjectDesc'] = 'Desc 3';

The function:

$('#addListObjectSubmit').click(function (e) {

            var listObjectName = $('#m_newListObject').val();

            if((listObjectName == null) || (listObjectName == '')) {
                return false;
            }
            else {
                listObjects['listObjectName'] = listObjectName;

                var listObjectDesc = $('#m_newListObjectDesc').val();

                if ((listObjectDesc == null) || (listObjectDesc == '')) {
                    listObjects['listObjectDesc'] = null;
                }
                else {
                    listObjects['listObjectDesc'] = listObjectDesc;
                }
            }

            e.preventdefault();
        });

So, what is the best way of dealing with this?

那么,处理这个问题的最佳方法是什么?

2 个解决方案

#1


4  

It will help you a bit if you forget about associative arrays. They only theoretically exist in Javascript. In fact, everything is an object, even arrays. But thinking in data storage terms, you can use a simple array that can only be indexed numerically, or you can use an object as a data map.

如果忘记关联数组,它会对你有所帮助。它们理论上只存在于Javascript中。事实上,一切都是一个对象,甚至是数组。但是考虑数据存储术语,您可以使用只能以数字方式编制索引的简单数组,或者可以将对象用作数据映射。

The following example creates an array (note the more compact [] instead of new Array()) and pushes a map (created with {}) into it:

下面的示例创建一个数组(注意更紧凑的[]而不是new Array())并将地图(使用{}创建)推入其中:

var listObjects = [];
...
var newElem = {
    'listObjectName' : 'Name 1',
    'listObjectDesc' : 'Desc 1'
};
listObjects.push(newElem);

Afterwards, you can access this element with listObjects[0], if it was the first element in the array.

之后,如果它是数组中的第一个元素,则可以使用listObjects [0]访问此元素。

If you want to access its properties, you can use one of these:

如果要访问其属性,可以使用以下方法之一:

listObjects[0].listObjectName
listObjects[0]['listObjectName']

So you can see that when dealing with objects, you can use the . notation as well as the bracket notation - they are equivalent, but the latter form makes it look like it is an "associative array" (even more for people coming from PHP).

所以你可以看到,在处理对象时,你可以使用。符号以及括号表示法 - 它们是等价的,但后一种形式使它看起来像是一个“关联数组”(对于来自PHP的人来说更是如此)。

#2


2  

..you mean, you want to add an "associative array" (they're called "objects", in JavaScript, btw) to an array on each click?

..你的意思是,你想在每次点击时向一个数组添加一个“关联数组”(它们在JavaScript中被称为“对象”,btw)?

The method to add things to arrays is push():

将内容添加到数组的方法是push():

listObject.push({
    'listObjectName': 'Name X',
    'listObjectDesc': 'Description X'
})

#1


4  

It will help you a bit if you forget about associative arrays. They only theoretically exist in Javascript. In fact, everything is an object, even arrays. But thinking in data storage terms, you can use a simple array that can only be indexed numerically, or you can use an object as a data map.

如果忘记关联数组,它会对你有所帮助。它们理论上只存在于Javascript中。事实上,一切都是一个对象,甚至是数组。但是考虑数据存储术语,您可以使用只能以数字方式编制索引的简单数组,或者可以将对象用作数据映射。

The following example creates an array (note the more compact [] instead of new Array()) and pushes a map (created with {}) into it:

下面的示例创建一个数组(注意更紧凑的[]而不是new Array())并将地图(使用{}创建)推入其中:

var listObjects = [];
...
var newElem = {
    'listObjectName' : 'Name 1',
    'listObjectDesc' : 'Desc 1'
};
listObjects.push(newElem);

Afterwards, you can access this element with listObjects[0], if it was the first element in the array.

之后,如果它是数组中的第一个元素,则可以使用listObjects [0]访问此元素。

If you want to access its properties, you can use one of these:

如果要访问其属性,可以使用以下方法之一:

listObjects[0].listObjectName
listObjects[0]['listObjectName']

So you can see that when dealing with objects, you can use the . notation as well as the bracket notation - they are equivalent, but the latter form makes it look like it is an "associative array" (even more for people coming from PHP).

所以你可以看到,在处理对象时,你可以使用。符号以及括号表示法 - 它们是等价的,但后一种形式使它看起来像是一个“关联数组”(对于来自PHP的人来说更是如此)。

#2


2  

..you mean, you want to add an "associative array" (they're called "objects", in JavaScript, btw) to an array on each click?

..你的意思是,你想在每次点击时向一个数组添加一个“关联数组”(它们在JavaScript中被称为“对象”,btw)?

The method to add things to arrays is push():

将内容添加到数组的方法是push():

listObject.push({
    'listObjectName': 'Name X',
    'listObjectDesc': 'Description X'
})