【问题标题】:Is there a way to shuffle json value-pair's有没有办法改组 json 值对的
【发布时间】:2020-03-21 01:00:19
【问题描述】:

我有一组键值对,其中有图像的 url,这个 json 文件保存在 firebase 的实时数据库中。我的问题是我们可以在桌面中导出 json 后重新排序值(url's)和在记事本++中打开?还是有其他方法?

如果我们可以像本例中那样重新排序值,单独的一对有 3 个 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.- 这只是一个示例文件,因此仅给出了几个文件,实际文件太大而无法在记事本++中手动随机播放

【问题讨论】:

  • “随机播放”到底是什么意思?您能否编辑问题以更好地说明您希望看到的情况?
  • 已编辑,如果清楚请告诉我
  • 您已经展示了一个 JSON sn-p,但还不是您想要的样子。另请注意,查看您已经尝试过的内容确实会有所帮助。

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


【解决方案1】:

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

【讨论】:

  • 是的,但实际文件包含很多行,重新排序需要很长时间。
【解决方案2】:

我了解到您希望对数组进行打乱,以便每次以不同的顺序显示图片。

你会发现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));

【讨论】:

  • 我应该在哪个 IDE 上运行它?
  • 这只是 JavaScript 代码。因此,您可以在前端执行它(如果它是一个 Web 应用程序),也可以在云函数中执行它。
  • 那么我应该通过notepad++运行这个吗?因为它在实时数据库中,我无法将代码放在实时数据库中自动运行。
  • 这完全取决于您是要执行一次还是每次用户查询图像列表时执行此操作。
  • 如何将这个程序添加到firebase的实时数据库中?
猜你喜欢
  • 2021-09-27
  • 1970-01-01
  • 2016-11-24
  • 2019-09-04
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
相关资源
最近更新 更多