gpt4 book ai didi

javascript - 使用 jQuery.map() 函数创建自定义对象数组

转载 作者:行者123 更新时间:2023-12-01 02:29:54 25 4
gpt4 key购买 nike

enter image description here
我有如上所示的表格,我想创建一个自定义数组来传递值。

目前我正在使用以下代码行:

var arr = $('input[type=text].editfield').map(function () {
return this;
}).get();
var objArr = jQuery.map(arr, function (i) {
return {
myDate: i.parentElement.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.childNodes[0].textContent,
myValue: i.value
}
}).get();

我希望网格中的所有项目都有一个对象数组,分别以日期和值作为属性。

但是有些问题我无法解决。例如,上面的代码显示“jQuery.map(...).get 不是函数

如何更正我的代码以执行正确的操作?

最佳答案

无需在静态 jQuery.map() 上使用 .get()函数,它返回一个正确的数组,而插件方法 .map()返回一个 jQuery 对象,您必须对其调用 .get()获取一个数组。

<小时/>

也不需要使用 2 个循环,

var objArr = $('input[type=text].editfield').map(function (idx, i) {
//the element selection used here is cruel, if you can share the html used we can help you to clean it
return {
// you can try myDate: $(this).parent().prevAll().eq(5).text() - not testable without the html and what is the expected output
myDate: i.parentElement.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.childNodes[0].textContent,
myValue: i.value
};
}).get();

关于javascript - 使用 jQuery.map() 函数创建自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426947/

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