gpt4 book ai didi

javascript - 在javascript中形成数组值

转载 作者:行者123 更新时间:2023-11-30 08:01:45 24 4
gpt4 key购买 nike

我正在尝试形成以下结构

{
"details": [
{
"id": 1,
"index": "index"
},
{
"id": 2,
"index": "index"
}
],
"user": "user"
}

为此我正在努力

 var order = [];
$('.movables').each(function(index, element){
var id = $(this).children().attr('id');
order.push({'id': id, 'index' : index});
});
order.push('user',user);

它给出了错误的格式。我不知道如何形成上述结构。

我正在从每个方法中获取 id、索引值。它工作正常。我正在获取 ID、索引和用户值,但我不知道如何形成

最佳答案

order 应该是一个对象,而不是一个数组,包含一个数组的 details 属性。

var order = {details: []};
$('.movables').each(function(index, element){
var id = $(this).children().attr('id');
order.details.push({'id': id, 'index' : index});
});
order.user = user;

关于javascript - 在javascript中形成数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418505/

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