-2

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}]
3
  • 1
    what is the condition? what have you tried? Commented Jul 26, 2020 at 8:28
  • I tried using 2 maps but found that i would be iterating through twice , i also tried thought of using the append to append it to a new array , but feels that there is a better solution. The condition is to check if assigned is null Commented Jul 26, 2020 at 8:32
  • Loop through the array and based on the condition, push them to 2 different arrays Commented Jul 26, 2020 at 8:34

6 Answers 6

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

Would it be possible to create a new constant based on the onces that failed the filter? Because that would mean im iterating through the array less
@neowenshun, I don't really follow what you mean? If the condition isn't met, then you get an empty array[].
0

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}]

Comments

0

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);

Comments

0

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)

Comments

-3

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.