javascript - 随机化特征集合

标签 javascript arrays node.js random

我有一个 geojson 特征集合,整个集合中包含如下特征。

{ geometry: { coordinates: [Array], type: 'LineString' },
properties: { angle: 120.2},
type: 'Feature' }

有没有一种方法可以随机化集合中的特征并使它们在 JavaScript 中不连续?

最佳答案

您可以将Array#sortMath.random结合使用。

const features = [{
  geometry: {
    coordinates: [Array],
    type: 'LineString'
  },
  properties: {
    angle: 120.2
  },
  type: 'Feature'
}, {
  geometry: {
    coordinates: [Array],
    type: 'LineString'
  },
  properties: {
    angle: 100.2
  },
  type: 'Feature'
}, {
  geometry: {
    coordinates: [Array],
    type: 'LineString'
  },
  properties: {
    angle: 4.2
  },
  type: 'Feature'
}, {
  geometry: {
    coordinates: [Array],
    type: 'LineString'
  },
  properties: {
    angle: 15.2
  },
  type: 'Feature'
}]

const randomSort = (arr) => {
  return arr.sort(() => {
    return 0.5 - Math.random()
  })
}

console.log(randomSort(features))

关于javascript - 随机化特征集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47165767/

相关文章:

javascript - 在 Javascript 中将函数更改为同步执行

javascript - 在 typescript 中导入文件的正确方法是什么?

javascript - 当 keyup 事件(对于另一个键)被触发时,keydown(重复)中断

python - 将 Pandas 数据帧的行映射到 numpy 数组

ruby - 如何检查数组中是否设置了 nil?

javascript - 在 Meteor 中使用 AutoForm 可编辑表格

javascript - 卡住了nodejs回调

javascript - 在 bootstrap 中使用 Collapsibles

python - 求和小于给定阈值时的三元组总数

node.js - 获取或搜索 npm 中的可执行文件列表