0

I want to filter one value from my first array and create a second array with the filtered value.

So far I have that but it does not seem very efficient.

  const blacklist = bookingsList.filter(booking => booking.id === id);
  const newBookingList = bookingsList.filter(booking => booking.id !== id);

Is there a better way to do this?

1
  • @Mamun Well I am not sure, but it feels I am Iooping twice over bookingsList Commented Oct 11, 2018 at 11:08

6 Answers 6

2

I think something like this would be good on a large array or if testing the condition is expensive because you would only loop through the array once

const array1 = [];
const array2 = [];
for (var i = 0; i < input.length; i++) {
    const value = input[i];
    ( testCondition(value) ? array1 : array2 ).push(value);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it with a single iteration by using forLoop like

const blacklist = [];
const newBookingList = [];
bookingsList.forEach(booking => {
     if(booking.id === id) {
         blacklist.push(booking)
     }
     else {
         newBookingList.push(booking)
     }
}

Comments

1

You can use forEach() and ternary operator:

const bookingsList = [{id:'black'},{id:'new'}];
const blacklist = [], newBookingList = [], id='black';
bookingsList.forEach(booking => booking.id === id? blacklist.push(booking.id) : newBookingList.push(booking.id));

console.log(blacklist);
console.log(newBookingList);

Comments

1

let blacklist =  []
let newBookingList = []
const ID = 10;
let bookingsList=[{id:10}, {id:20}]

bookingsList.forEach(booking => booking.id === ID ? blacklist.push(booking) : newBookingList.push(booking))


console.log(newBookingList)
console.log(blacklist)

Comments

0

I think you can use forEach for that:

const newBookingList = [];
const blacklist = [];
bookingsList.forEach(function(booking) {
  if(booking.id === id){
      blacklist.push(booking)
  }
  if(booking.id !== id){
      newBookingList.push(booking)
  }
})

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

1 Comment

if(booking.id === id) and if(booking.id !== id)? It's pretty strange to see both conditions - a simple if...else would implicitly be the same and it seems less out of place. Right now it sort of seems like you expect to have an ID that is at the same time not equal to ID failing the first test and also is also not not equal (therefore equal) to ID failing the second test.
0

You can use splice method to retrive and remove filtered value.

 var bookingList = [{id : 1, name : "A"}, {id: 2, name: "B"}];
 var sBookingList = bookingList.splice(bookingList.map(function(b){return b.name}).indexOf("A"), 1);
    console.log(sBookingList);
    console.log(bookingList);

2 Comments

So, what happens if the input has 1000 items?
then its not worked. Use filter method to retrieve values.

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.