javascript - 将 JSON 数据数组加一
问题描述
我想知道为什么这个 i+=1 不会将数组增加 1。当我单击按钮时,会显示整个数组长度。有人可以告诉我我做错了什么。我正在尝试从 WP 加载与特定类别的帖子相关的 JSON 数据。我正在加载的这个类别(http://localhost:8888/wordpress/wp-json/wp/v2/photos?categories=12)总共有 2 个帖子。当我 console.log postsData.length 时,我得到 2 的值。当我控制台 log postsData 时,我收到 Object、Object Object、Object。这些帖子都在单击按钮时同时加载,但我希望帖子或每个对象一次加载 1 个。下面是代码:
var portfolioPostsBtn = document.getElementById("portfolio-posts-btn");
var portfolioPostsContainer = document.getElementById("portfolio-posts-container");
if (portfolioPostsBtn){
portfolioPostsBtn.addEventListener("click", function(){
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'http://localhost:8888/wordpress/wp-json/wp/v2/photos?categories=12');
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
//console.log(data);
createHTML(data);
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
});
}
function createHTML(postsData){
var ourHTMLString = '';
// This for loop doesn't increment by 1
for (var i=0; i<postsData.length; i += 1){
console.log(postsData.length);
ourHTMLString += '<h2>' + postsData[i].title.rendered + '</h2>';
ourHTMLString += '<img class="gallery-test" src="' +postsData[i].acf.image + '"">'
}
portfolioPostsContainer.innerHTML = ourHTMLString;
}
解决方案
让我试着正确理解你的问题。如果您尝试每次点击只显示一篇文章。这是我认为您可以尝试的方法:
let portfolioPostsBtn = document.getElementById("portfolio-posts-btn");
let portfolioPostsContainer = document.getElementById("portfolio-posts-container");
// keep the payload to avoid unnecessary data requests
let postsData = [];
// using this to track click history to display the next post
let counter = 0;
/** not needed for disabling the button **/
// cycle (postsData.length) one by one starting at index 0
// for ex, if there are 3 posts, it would do 0, 1, 2, 0, 1, 2, to eons
// const incrementCounter = () => {
// counter = (counter + 1)%(postsData.length)
// };
// create post html if there is postsData
postsData.length && createHTML();
if (portfolioPostsBtn && !postsData.length){
portfolioPostsBtn.addEventListener("click", function(){
let ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'http://localhost:8888/wordpress/wp-json/wp/v2/photos?categories=12');
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
let data = JSON.parse(ourRequest.responseText);
//console.log(data);
postsData = data;
createHTML();
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
});
}
function disableButton () {
// this would only work if portfolio-posts-btn is an actual button,
// otherwise, you'll need to disable the listener and show the
// user the btn is disabled somehow
portfolioPostsBtn.setAttribute('disable', true);
}
function createHTML() {
const strTemplate = `
<h2>${postsData[counter].title.rendered}</h2>
<img class="gallery-test" src="${postsData[counter].acf.image}">`;
// append to the added posts
portfolioPostsContainer.innerHTML += strTemplate;
counter++;
postsData.length === counter && disableButton();
}