gpt4 book ai didi

javascript - 循环输入值并将它们按一定顺序放入对象内

转载 作者:行者123 更新时间:2023-12-02 16:12:45 24 4
gpt4 key购买 nike

我试图从隐藏的输入字段中获取值并将它们粘贴到一个可以通过 AJAX 发布到 url 的对象中。我能够获取这些值,但无法按照我需要的顺序获取它们。我正在获取这样的值:

var music = {};

var album_name = $(".album_name").each(function() {
music[$(this).attr("value")] = $(this).val();
// console.log($(this).val());
});
$(".song_title").each(function() {
music[$(this).attr("value")] = $(this).val();
// console.log($(this).val());
});
$(".album_image").each(function() {
music[$(this).attr("value")] = $(this).val();
console.log($(this).val());
});

结果如下:

{"Album 1":"Album 1","Album 2":"Album 2","Album 3":"Album 3","Album 4":"Album 4","Song 1":"Song 1","Song 2":"Song 2","Song 3":"Song 3","Song 4":"Song 4","thumbnail.png":"thumbnail.png"}

有没有办法让对象以这种类型的顺序出现:

{"Album":"Album 1","Song":"Song 1","Image":"thumbnail.png","Album":"Album 2","Song":"Song 2","Image":"thumbnail.png","Album":"Album 3","Song":"Song 3","Image":"thumbnail.png","Album":"Album 4","Song":"Song 4","Image":"thumbnail.png",}

预先感谢您的帮助。

最佳答案

正如安迪指出的那样,物体本质上是无序的。但是,如果您使用二维数组,则与“1”相关的值可以作为数组出现在第一个值中。

例如:

[{"Album":"Album 1","Song":"Song 1","Image":"thumbnail.png"},{"Album":"Album 2","Song":"Song 2","Image":"thumbnail.png"},{"Album":"Album 3","Song":"Song 3","Image":"thumbnail.png"},{"Album":"Album 4","Song":"Song 4","Image":"thumbnail.png"}]

关于javascript - 循环输入值并将它们按一定顺序放入对象内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968988/

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