我有一个N长度的数组。我需要创建一个重复序列的新数组。新数组将具有与N数组中的条目对应的子集数组,如:
N0 = [1,0,0]
N1 = [0,1,0]
N2 = [0,0,1]
N3 = [1,0,0]
N4 = [0,1,0];
reapeat...
预期输出:[[1,0,0],[0,1,0],[0,0,1],[1,0,0][0,1,0][0,0,1], repeating...]
我在如何做到这一点上空白我的第一个想法是在每个元素的索引上使用模运算符
positions = [[98,8097,709], [8,907,098], [234,543w6,098], [098,087,08089], "LOL", {cool: bro}, function(){console.log(derp);}]
positions.map((pos, idx) => {
if(idx % 1){
return [1,0,0];
}
if(idx % 2){
return [0,1,0];
}
if(idx % 3){
return [0,0,1];
}
})
答案 0 :(得分:1)
在new Array(length).fill(0)
中使用map()
,然后使用模数设置1
let positions = [
[98, 8097, 709],
[8, 907, 098],
[234, '543w6', 098],
[098, 087, 08089], "LOL", {
cool: 'bro'
},
function() {
console.log('derp');
}
]
let res = positions.map((pos, idx) => {
let subArr = new Array(3).fill(0)
subArr[idx % 3] = 1
return subArr
})
console.log(res)
答案 1 :(得分:0)
好的,在这里留下我自己的答案,因为这已经脱离了目标
我发布了一个答案,我从中复制了这个。它被低估了-3并删除了我不确定作者是谁
> [43,534,76,87,97,89,78,67,7645,6534,54,54,546,657,675,68].map(function(val, idx){ var ret = [0,0,0]; ret[idx % 3] = 1; return ret})
[ [ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ] ]
答案 2 :(得分:-1)
public class MyLog extends RecursiveTreeObject<MyLog> {
// more properties...
public final ObjectProperty<Image> statusImage;
public MyLog(String imagePath) {
this(new Image(MyProject.class.getResourceAsStream(imagePath)));
}
/**
* Constructor for passing Image objects.
* (Could be helpful to reuse Images to reduce the footprint.)
*/
public MyLog(Image statusImage) {
this.statusImage = new SimpleObjectProperty(statusImage);
}
}