gpt4 book ai didi

javascript - 这个辅助 $.each() 函数是如何赋值的?

转载 作者:行者123 更新时间:2023-11-30 17:25:38 25 4
gpt4 key购买 nike

有人能解释一下这个功能的一部分是如何运作的吗?

var result = {}

$.each(this.serializeArray(), function(i, v) {
result[v.name] = v.value;
});

从上面的代码中,我不理解以下内容:

result[v.name] = v.value;

我不明白这是如何给出我得到的结果的,这是一个具有名称值对的对象。这是如何工作的?

最佳答案

// result is defined as empty object
var result = {}


// your each binding most likely is inside a $('form').submit(function(){ in here });
// so this.searializeArray() converts ( whatever "this" is in your scope ) an JavaScript array of objects you iterate over where function(i <- is the key, v <- is the object)

$.each(this.serializeArray(), function(i, v) {
// so for every object in your array you take the value and assign a new object in your initial empty result object with object.name as key and object.value as value
result[v.name] = v.value;
});

// so result may look like this after that

result == {
"fooname":"foovalue",
"barname":1337
}

关于javascript - 这个辅助 $.each() 函数是如何赋值的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24339910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com