有没有一种方法可以洗牌JSON值对的

时间:2019-11-25 17:09:02

标签: arrays json firebase firebase-realtime-database shuffle

我有一组键值对,其中有图像的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",],
  }
}

P.S。-这只是一个示例文件,因此仅给出了少数几个文件,实际文件太大而无法在记事本++中手动将其洗牌

2 个答案:

答案 0 :(得分:0)

根据定义,JSON对象中键的顺序未定义。因此,尽管您可以对导出的文本文件中的行进行重新排序,但是当再次解析该文件时,并不一定会影响JSON对象。

答案 1 :(得分:0)

我了解您要对数组进行混洗,以便每次以不同的顺序显示图片。

您会发现in this SO question种不同的方法来随机排列数组。


以下代码将随机播放URL对象的每个数组。

  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));