我有一个如下所示的javascript数组:
var arr = [ {RoomId:1, RoomName: 'ABC'},
{RoomId:1, RoomName: 'ABC'},
{RoomId:1, RoomName: 'ABC'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
];
我的要求类似于
所以我的最终阵列就像:
var arr = [ {RoomId:1, RoomName: 'ABC'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
];
答案 0 :(得分:2)
您可以使用linq.js来维护此类任务
喜欢这个
使用RoomId查找RoomId = 1的特定房间。
代码
var data =Enumerable.From(arr).Where(function(x){return x.RoomId===1;}).ToArray();
从中移除最后几个元素,例如删除最后2个元素 RoomId = 1。
var data =Enumerable.From(arr).Where(function(x){return x.RoomId===1;}).OrderByDescending(function(x){return x.RoomId;}).Skip(2);
arr=data.OrderBy(function(x){return x.RoomId;}).ToArray();
答案 1 :(得分:0)
我为您的查询开发了逻辑,如
var destArr=[];
var flag=false;
var count=0;
for(i in arr)
{
if(arr[i].RoomId==1 && flag==false)
{
flag = true;
destArr[count++] = arr[i];
}
else if(flag==true && arr[i].RoomId!=1)
{
destArr[count++] = arr[i];
}
}
console.log(destArr);