我有一个 geojson 特征集合,整个集合中包含如下特征。
{ geometry: { coordinates: [Array], type: 'LineString' },
properties: { angle: 120.2},
type: 'Feature' }
有没有一种方法可以随机化集合中的特征并使它们在 JavaScript 中不连续?
最佳答案
您可以将Array#sort
与Math.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/