I have a filter method that filters an item from an array using an if
condition. Using the filterArray
I then use map method.
I would like to add a second condition and push a new array called OLD_ITEMS
to the ITEMS
array. How would I go about doing this?
import { OLD_ITEMS, ITEMS } from './constants';
let filterArray = ITEMS.filter(item => {
if (item.id === 'three') {
return false;
}
return true;
});
// TODO: add second condition here to push `OLD_TIMES` to `ITEMS`
const options = Object.assign(
...filterArray.map(({ id, name }) => ({ [id]: name }))
);
I have a filter method that filters an item from an array using an if
condition. Using the filterArray
I then use map method.
I would like to add a second condition and push a new array called OLD_ITEMS
to the ITEMS
array. How would I go about doing this?
import { OLD_ITEMS, ITEMS } from './constants';
let filterArray = ITEMS.filter(item => {
if (item.id === 'three') {
return false;
}
return true;
});
// TODO: add second condition here to push `OLD_TIMES` to `ITEMS`
const options = Object.assign(
...filterArray.map(({ id, name }) => ({ [id]: name }))
);
Share
Improve this question
asked Jul 26, 2018 at 19:10
kayeekayee
3402 gold badges6 silver badges21 bronze badges
5
-
1
You can do just
ITEMS.filter(item => item.id !== 'three')
– bigless Commented Jul 26, 2018 at 19:15 - Would you like to use concat as described here stackoverflow./a/351421/90997 ? – Mikhail Aksenov Commented Jul 26, 2018 at 19:16
-
So you want to add all of old items to the
filteredArray
after you filter it? – Garrett Motzner Commented Jul 26, 2018 at 19:17 -
1
What's the second condition for adding the
OLD_TIMES
array? If the same filter predicate applies, you can just doTIMES.concat(OLD_TIMES).filter(item => item.id !== 'three')
. – FK82 Commented Jul 26, 2018 at 19:18 - Could you update, please? – Aidan Hoolachan Commented Aug 26, 2018 at 0:27
1 Answer
Reset to default 4You need to be a little more clear about what you mean by "push" OLD_ITEMS to Items. Do you want to concatenate/push ALL of the OLD_ITEMS to the end of if a single condition is met, or do you want to push subset of OLD_ITEMS that meet a certain condition?
I believe that this is what you're looking for, but it's hard to know exactly:
import { OLD_ITEMS, ITEMS } from './constants';
let filterArray = ITEMS.filter(item => {
if (item.id === 'three') {
return false;
}
return true;
});
// TODO: add second condition here to push `OLD_TIMES` to `ITEMS`
const validOldItems = OLD_ITEMS.filter(item => {
if (item === 'some_condition') {
return false;
}
return true;
}
filterArray.push(validOldItems);
const options = Object.assign(
...filterArray.map(({ id, name }) => ({ [id]: name }))
);
Also, I highly remend making your code more concise by returning the value of the conditional check, instead of an if/then
let filterArray = ITEMS.filter(item => {
return (item.id === 'three');
});
Or even more concise
let filterArray = ITEMS.filter(item => (item.id === 'three'));
Grand finale of conciseness:
const filterArray = ITEMS.filter(item => (item.id === 'three'))
.concat(OLD_ITEMS.filter(item => (item.id === 'some_condition'))
.map(({ id, name }) => ({ [id]: name }))
const options = Object.assign(...filterArray);