[英]How to dynamically add id property in an array of objects?
I have a simple section in which a user can add movie details using a form.我有一个简单的部分,用户可以在其中使用表单添加电影详细信息。 I am taking the values entered by the user and creating an object from them which I pushing in to an array.我正在获取用户输入的值并从中创建一个对象,然后将其推送到数组中。 However I also want to include a dynamic ID for each added movie.但是,我还想为每个添加的电影包含一个动态 ID。
$("#btnAddMovie").click(function(e) {
console.log('btn-movie triggered');
var jsonData = [];
var formData = $("#movieForm").serializeArray();
$.each(formData, function() {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: '1'
});
});
console.log(jsonData);
var params = JSON.stringify({
movies: jsonData
});
$.ajax({
type: 'POST',
data: {
params: params
},
url: 'save_to_json.php',
success: function(data) {
$('#msg').html(data).fadeIn('slow');
$('#msg').delay(2000).fadeOut('slow');
},
error: function() {
$('#error-msg').html(data).fadeIn('slow');
$('#error-msg').delay(2000).fadeOut('slow');
}
});
});
The above solution gives me this上面的解决方案给了我这个
{
"movies": [{
"title": "fdfdfd",
"movieurl": "http:\/\/meed.audiencevideo.com\/videos",
"movieid": "1"
}, {
"title": "fdfdfd",
"movieurl": "http:\/\/meed.audiencevideo.com\/videos",
"movieid": "1"
}]
}
I want movieid
to increment dynamically eg movieid: 1
, movieid: 2
etc. What do I need to do to get what I want?我希望movieid
动态递增,例如movieid: 1
、 movieid: 2
等。我需要做什么才能得到我想要的?
From your code, you can use the index in the callback in the $.each
从代码中,您可以在$.each
的回调中使用索引
....
$.each(formData, function(index) {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: index + 1
});
});
...
To understand the callback, see the jquery documentation https://api.jquery.com/jQuery.each/ 要了解回调,请参见jquery文档https://api.jquery.com/jQuery.each/
Alternatively you can also use the jsonData.length
或者,您也可以使用jsonData.length
....
$.each(formData, function(index) {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: jsonData.length + 1
});
});
...
Try this: 尝试这个:
$.each(formData, function () {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: jsonData.length ? jsonData[jsonData.length - 1].movieid + 1 : 0,
});
});
or 要么
$.each(formData, function () {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: jsonData.length,
});
});
These are just simple examples that can only be used if you are not gonna delete items from an array. 这些只是简单的示例,仅当您不打算从数组中删除项目时才可以使用。
You can put a parameter in the callback function. 您可以在回调函数中放置一个参数。 The parameters are (index, array), but you would only need index. 参数是(索引,数组),但是您只需要索引。
$.each(formData, function(i) {
console.log(formData);
jsonData.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: i + 1
});
});
Most of the solution provided by friends here are not well constructed, they don't give a perfect solution to my problem. 这里的朋友提供的大多数解决方案的结构都不完善,无法为我的问题提供完美的解决方案。
Most of the common problems were pointed out by @Randy Casburn @Randy Casburn指出了大多数常见问题
Here is my solution I found out working perfectly, I thought it will be good to share, maybe will help someone out there. 这是我发现可以完美工作的解决方案,我认为分享会很好,也许会帮助某个人。
my json file looks like this 我的json文件看起来像这样
{
"movies": [
{
"title": "sdsds",
"movieurl": "https:\/\/meed.audiencevideo.com\/videos\/good_job.mp4",
"movieid": 1,
"buttons": []
},
{
"title": "sdsds",
"movieurl": "https:\/\/meed.audiencevideo.com\/videos\/do_you_want.mp4",
"movieid": 2,
"buttons": []
},
]
}
Here is my js 这是我的js
function getNextMovieId() {
var nextmovieid = 0;
for (var a = 0; a < movies.length; a++) {
if( movies[a].movieid>nextmovieid ) {
nextmovieid = movies[a].movieid;
}
}
nextmovieid++;
return nextmovieid;
}
$("#btnAddMovie").click(function(e){
var formData = $("#movieForm").serializeArray();
console.log(formData);
movies.push({
title: formData[0].value,
movieurl: formData[1].value,
movieid: getNextMovieId(),
buttons: []
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.