簡體   English   中英

有沒有辦法洗牌 json 值對的

[英]Is there a way to shuffle json value-pair's

我有一組鍵值對,其中有圖像的 url,這個 json 文件保存在 firebase 的實時數據庫中。我的問題是在桌面導出 json 並在記事本 ++ 中打開后,我們可以重新排序值(url) ? 或者還有其他方法嗎?

如果我們可以像在這個例子中那樣對值重新排序,單獨的一對有 3 個 url,如果我們可以重新排序它們,第一個變成第三個,第二個變成第一個,那么應用程序上的最終用戶可以以不同的順序看到照片每次我們在數據庫中更改/重新排序/洗牌 url 時。 因為只有三個 url 對我們來說很容易,我們可以手動完成,而且不費時,想象一下單獨的一對有 100 個 url,然后很難手動更改每個 url,因為它既耗時又很多更令人困惑

json文件的一個例子是-

{
  "URLs" : {
    "ALONE" : [ "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/alone.jpg?alt=media&token=1f8ee2bd-7bd2-4223-b708-2ae48ab3c5da", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/6094594.jpg?alt=media&token=2c2611af-6e15-4d8e-8d4e-32fa347d8025", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/8620007.jpg?alt=media&token=c54ec2ff-8c15-41ba-b0f1-e9d256586ce2",],
    "AMAZING" : [ "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/amazing.jpg?alt=media&token=381900b3-cab9-4802-a9db-6d8b938d3d16", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/7514001.jpg?alt=media&token=1e424124-103f-4bbe-b28d-736896d3afe4", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/4546211.jpg?alt=media&token=35fbf774-4262-4c45-8e7d-8efbaafabd29",],
  }
}

PS-這只是一個示例文件,所以只有少數給出了實際文件太大而無法在記事本++中手動洗牌

JSON 對象中鍵的順序是未定義的。 因此,雖然您可以對導出的文本文件中的行重新排序,但這不一定會在再次解析文件時影響 JSON 對象。

我知道您想對數組進行洗牌,以便每次以不同的順序顯示圖片。

你會在這個 SO question 中找到不同的方法來混洗數組。


以下代碼將打亂 URLs 對象的每個數組。

  function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  const rawObj = {
    URLs: {
      ALONE: [
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/alone.jpg?alt=media&token=1f8ee2bd-7bd2-4223-b708-2ae48ab3c5da',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/6094594.jpg?alt=media&token=2c2611af-6e15-4d8e-8d4e-32fa347d8025',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/8620007.jpg?alt=media&token=c54ec2ff-8c15-41ba-b0f1-e9d256586ce2'
      ],
      AMAZING: [
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/amazing.jpg?alt=media&token=381900b3-cab9-4802-a9db-6d8b938d3d16',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/7514001.jpg?alt=media&token=1e424124-103f-4bbe-b28d-736896d3afe4',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/4546211.jpg?alt=media&token=35fbf774-4262-4c45-8e7d-8efbaafabd29'
      ]
    }
  };

  const urlsObj = rawObj.URLs;
  const keys = Object.keys(urlsObj);
  for (const key of keys) {
    console.log(urlsObj[key]);
    console.log(shuffleArray(urlsObj[key]));
  }

如果您希望將改組應用於所有數組所有元素,您可以執行以下操作:

  function shuffleArray(array) {
    //.....
    return array;
  }

  const rawObj = {...};

  const urlsObj = rawObj.URLs;
  const keys = Object.keys(urlsObj);

  let fullArray = [];
  for (const key of keys) {
    fullArray = fullArray.concat(urlsObj[key]);
  }
  console.log(fullArray);
  console.log(shuffleArray(fullArray));

暫無
暫無

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

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