Reputation: 4479
I have a javascript array like below:
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'},
];
My requirement is something like
So my final array would be like:
var arr = [ {RoomId:1, RoomName: 'ABC'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
{RoomId:2, RoomName: 'XYZ'},
];
Upvotes: 2
Views: 67
Reputation: 25352
You can use linq.js to maintain this kind of task
Like this
Find particular room with RoomId say RoomId = 1.
Code
var data =Enumerable.From(arr).Where(function(x){return x.RoomId===1;}).ToArray();
Remove last few elements from it like remove last 2 elements with 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();
Upvotes: 2
Reputation: 18600
I developed logic for your query like
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);
https://jsfiddle.net/oz6hw8qd/
Upvotes: 0