假设我有一个Javascript数组,如下所示:
["Element 1","Element 2","Element 3",...]; // with close to a hundred elements.
Run Code Online (Sandbox Code Playgroud)
什么方法适合将数组块(拆分)到许多较小的数组中,比如最多10个元素?
有谁知道如何通过节点redis获得分数?我试过这样的事情:
client.ZRANGE(key, 0, -1, withscores, function(err, replies) {
});
Run Code Online (Sandbox Code Playgroud)
谢谢.
下面是一个数组,我必须在每个对象中分组3个值
var xyz = {"name": ["hi","hello","when","test","then","that","now"]};
Run Code Online (Sandbox Code Playgroud)
输出应低于数组 -
["hi","hello","when"]["test","then","that"]["now"]
Run Code Online (Sandbox Code Playgroud) 假设我有以下数组:
const list = [{}, {}, {}, {}, {}, {}, {}, {}]
Run Code Online (Sandbox Code Playgroud)
使用lodash,我想将它们“分组”为三个,这样我就可以在react/js中对它们进行排序
// 映射分组对象
group =>
<div>
// Now map the three elements by one.
item =>
<div>item.value</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
const mediaList = _.groupBy(media, (element, index) => {
return Math.floor(index/3)
})
Run Code Online (Sandbox Code Playgroud)
不起作用,因为所有项目仍然分组到一个数组中,并且该数组的名称是NaN。
我怎样才能做到这一点?
说我有一个大阵列
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
Run Code Online (Sandbox Code Playgroud)
并希望将其拆分为一组像n元组一样的元组
[[1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14] /*, ... */ ] // (for n=2)
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来实现这一目标?特殊情况n = 2对我来说已经足够了.
假设我有一个数组:
var ay=[0,1,2,3,4,5,6,7,8,9];
Run Code Online (Sandbox Code Playgroud)
现在我想得到两个数组:
var ay1=[0,2,4,6,8];
var ay2=[1,3,5,7,9];
Run Code Online (Sandbox Code Playgroud)
有效的方法是什么?
更新:
我知道简单的循环和模运算符方法(如上所述elclanrs),如下所示:
var ay1=[],ay2=[];
for(var i=0,len=ay.length;i++){
if(i%2==0){
ay2.push(ay[i]);
} else
ay1.push(ay[i]);
}
Run Code Online (Sandbox Code Playgroud)
但我只是想知道是否还有其他有效或酷的方式我还不知道.
这就是为什么我问这个简单的问题.我不是问怎么做,我问如果可能的话怎么做得更好!
所以我认为这篇文章不值得投票.