簡體   English   中英

使用 function () 提取數據后,如何將 JSON object 數據存儲到變量中?

[英]How to store JSON object data into a variable once that data is extracted with a function ()?

我有一個 JSON 文件,里面有圖片庫。 我正在使用 function 提取該數據,但我希望一旦提取該數據以將其存儲到變量中,以便從現在開始我可以使用該變量進行數據引用。 我怎樣才能做到這一點

function getJsonphotos() {
    $.getJSON("./json/photos.json", displayImages);
}

function displayImages(data) {
data.forEach(function (item) {
    storeContainer.append(`<<img id=${item.id} src=${item.location} />)
  });

 let jsonArrayVariable = [] ; 

我應該如何將照片放在jsonArrayVariable變量中。

您在回調 function 之外創建一個數組。 然后將每個項目推送到數組中。

let jsonArrayVariable = [];
function displayImages(data) {
data.forEach(item => {
    storeContainer.append(`<img id=${item.id} src=${item.location} />`);
    jsonArrayVariable.push(item);
  });

我是這樣做的:

arrayImages = [];
function getJsonphotos(data) {
    $.getJSON("./json/photos.json",function (data){
    arrayImages = data; 
    displayImages(data);
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM