Javascript 通过 .push() 方法向对象添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7261431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding items to an object through the .push() method
提问by dperitch
I'm doing a loop through few input elements of 'checkbox' type. After that, I'm adding values and checked attributes to an array. This is my code:
我正在通过“复选框”类型的几个输入元素进行循环。之后,我将值和检查的属性添加到数组中。这是我的代码:
var stuff = {};
$('form input[type=checkbox]').each(function() {
stuff[$(this).attr('value')] = $(this).attr('checked');
});
This works fine, but I'm just wondering if I can do the exact same thing with .push() method in Jquery?
这很好用,但我只是想知道我是否可以用 Jquery 中的 .push() 方法做完全相同的事情?
I've tried something like this but it doesn't work:
我试过这样的事情,但它不起作用:
stuff.push( {$(this).attr('value'):$(this).attr('checked')} );
Edit:
编辑:
I was trying to use .push() method on Object, but .push() is actually just a method of Array Object.
我试图在 Object 上使用 .push() 方法,但 .push() 实际上只是 Array Object 的一个方法。
回答by jondavidjohn
.push()
is a method of the Built-in Array Object
It is not related to jQuery in any way.
它与 jQuery 没有任何关系。
You are defining a literal Objectwith
你要定义一个文本对象与
// Object
var stuff = {};
You can define a literal Arraylike this
您可以像这样定义文字数组
// Array
var stuff = [];
then
然后
stuff.push(element);
Arrays actually get their bracket syntax stuff[index]
inherited from their parent, the Object. This is why you are able to use it the way you are in your first example.
数组实际上stuff[index]
从它们的父对象 Object 继承了它们的括号语法。这就是为什么您可以像在第一个示例中那样使用它的原因。
This is often used for effortless reflection for dynamically accessing properties
这通常用于动态访问属性的轻松反射
stuff = {}; // Object
stuff['prop'] = 'value'; // assign property of an
// Object via bracket syntax
stuff.prop === stuff['prop']; // true
回答by Иван Олейник
so it's easy)))
所以很容易)))
Watch this...
看这个...
var stuff = {};
$('input[type=checkbox]').each(function(i, e) {
stuff[i] = e.checked;
});
And you will have:
你将拥有:
Object {0: true, 1: false, 2: false, 3: false}
Or:
或者:
$('input[type=checkbox]').each(function(i, e) {
stuff['row'+i] = e.checked;
});
You will have:
你将会有:
Object {row0: true, row1: false, row2: false, row3: false}
Or:
或者:
$('input[type=checkbox]').each(function(i, e) {
stuff[e.className+i] = e.checked;
});
You will have:
你将会有:
Object {checkbox0: true, checkbox1: false, checkbox2: false, checkbox3: false}
回答by ShankarSangoli
stuff
is an object and push
is a method of an array. So you cannot use stuff.push(..)
.
stuff
是一个对象,push
是一个数组的方法。所以你不能使用stuff.push(..)
.
Lets say you define stuff
as an array stuff = [];
then you can call push
method on it.
假设您定义stuff
为一个数组,stuff = [];
然后您可以对其调用push
方法。
This works because the object[key/value] is well formed.
这是有效的,因为 object[key/value] 格式良好。
stuff.push( {'name':$(this).attr('checked')} );
stuff.push( {'name':$(this).attr('checked')} );
Whereas this will not work because the object is not well formed.
而这将不起作用,因为对象没有很好地形成。
stuff.push( {$(this).attr('value'):$(this).attr('checked')} );
stuff.push( {$(this).attr('value'):$(this).attr('checked')} );
This works because we are treating stuff
as an associative array and added values to it
这是有效的,因为我们将其stuff
视为关联数组并向其添加值
stuff[$(this).attr('value')] = $(this).attr('checked');
stuff[$(this).attr('value')] = $(this).attr('checked');
回答by C47
This is really easy: Example
这真的很简单:示例
//my object
var sendData = {field1:value1, field2:value2};
//add element
sendData['field3'] = value3;
回答by Niklas Jarl
Another way of doing it would be:
另一种方法是:
stuff = Object.assign(stuff, {$(this).attr('value'):$(this).attr('checked')});
Read more here: Object.assign()
在此处阅读更多信息:Object.assign()