I have the following array:
activeTickets = [{id:1,assigned_to:null},{id:2,assigned_to:'john'}]
I wish to split it to 2 different arrays:
activeassignedtickets = [{id:2,assigned_to:john}]
activeunassignedtickets = [{id:1,assigned_to:null}]
I have the following array:
activeTickets = [{id:1,assigned_to:null},{id:2,assigned_to:'john'}]
I wish to split it to 2 different arrays:
activeassignedtickets = [{id:2,assigned_to:john}]
activeunassignedtickets = [{id:1,assigned_to:null}]
Since you seem to want to do one loop, you can consider using .reduce() with an accumulator which starts off as an array storing two empty arrays. Based on the number result of o.assigned_to === null you can decide which index to put the object into:
const tickets = [{id:1,assigned_to:null},{id:2,assigned_to:'john'}];
const [activeTickets, unassignedTickets] = tickets.reduce(
(arr, o) => (arr[+(o.assigned_to === null)].push(o), arr),
[[], []]);
console.log(unassignedTickets);
console.log(activeTickets);
You can simplify this to be just a loop though, which in my opinion is easier to understand and read. Here we're looping over each object in the array and checking the value of the assigned_to property. If assigned_to is null you can add it to the unassigned list, otherwise, if it isn't null, you can add it to the active list:
const tickets = [{id:1,assigned_to:null},{id:2,assigned_to:'john'}];
const activeTickets = [];
const unassignedTickets = [];
for(let o of tickets) {
if(o.assigned_to === null)
unassignedTickets.push(o);
else
activeTickets.push(o);
}
console.log(unassignedTickets);
console.log(activeTickets);
You can use the filter method to achieve this I believe.
const activeTickets = tickets.filter(ticket => ticket.assigned_to !== null)
const unassignedTickets = tickets.filter(ticket => ticket.assigned_to === null)
const tickets = [{id:1,assigned_to:null},{id:2,assigned_to:"john"}]
const activeTickets = []
const unassignedTickets= []
// Iterate only once
for(let item of tickets){
item.assigned_to? activeTickets.push(item) : unassignedTickets.push(item)
}
console.log(activeTickets) // [{id: 2, assigned_to: "john"}]
console.log(unassignedTickets) // [{id: 1, assigned_to: null}]
You could move the assigned to an array and the not assigned items to another array.
const
activeTickets = [{ id: 1, assigned_to: null }, { id: 2, assigned_to: 'john' }],
{ true: notAssigned, false: assigned } = activeTickets.reduce(
(r, o) => (r[o.assigned_to === null].push(o), r),
{ true: [], false: [] }
);
console.log(assigned);
console.log(notAssigned);
const tickets = [
{id:1,assigned_to:null},
{id:2,assigned_to:'john'}
]
const activeassignedtickets =[]
const activeunassignedtickets = []
for (let i = 0; i < tickets.length; i += 1) {
(tickets[i].assigned_to === null)
? activeunassignedtickets.push(tickets[i])
: activeassignedtickets.push(tickets[i])
}
console.log(activeassignedtickets, activeunassignedtickets)
In your case, you can use array.slice method.
So here are details about how to do that.
activeassignedtickets = activeTickets.slice(1, 2);
activeunassignedtickets = activeTickets.slice(0, 1);