1

How do I solve this while using include()?

const allowedIds = [1, 3]

const allBoats = [{
  id: 1,
  name: 'titanic'
}, {
  id: 2,
  name: 'anna'
}, {
  id: 3,
  name: 'boaty McBoatface'
}, ]

const expectedResult = ['boaty McBoatface', 'titanic']

3
  • 1
    Why is 'boaty McBoatface' first? Commented Nov 10, 2022 at 9:46
  • check includes insde filter and map the filtered result to get just name Commented Nov 10, 2022 at 9:46
  • 3
    Please don't vandalize your own posts. When you post here, you give SO the right to distribute the content under CC-by SA 4.0. Any vandalism will be reverted. Commented Nov 10, 2022 at 9:52

5 Answers 5

3

Consider making allowedIds a Set and then using has, which is O(1), rather than includes on an Array, which is O(N):

const allowedIds = new Set([1, 3]);
const allBoats = [
  {
    id: 1,
    name: "titanic",
  },
  {
    id: 2,
    name: "anna",
  },
  {
    id: 3,
    name: "boaty McBoatface",
  },
];
const allowedBoats = allBoats.filter(b => allowedIds.has(b.id))
                             .map(b => b.name)
                             .sort();
console.log(allowedBoats);

Other potentially useful documentation links:

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

Comments

2

const allowedIds = [1, 3]

const allBoats = [{
  id: 1,
  name: 'titanic'
}, {
  id: 2,
  name: 'anna'
}, {
  id: 3,
  name: 'boaty McBoatface'
}, ]

const result = allBoats
  .filter(b => allowedIds.includes(b.id))
  .map(b => b.name)
  .sort((a, b) => a.localeCompare(b))
console.log('result', result)

3 Comments

Please add some explanation as well as code (maybe add some documentation links for filter/map/sort) There's a good chance someone looking for this sort of solution isn't going to have a complete understanding of those methods.
I've made you a runnable code snippet, they're useful to show running code and its output: Stack Overflow & run code snippet?
Please use Sash Sinha's answer. Using Set improves performance
0

You can try this as well

allBoats.filter(entry => allowedIds.includes(entry.id)).map(item => item.name).sort()

1 Comment

This is functionally identical to dangarfield's answer: stackoverflow.com/a/74387026/1650337
0

const allowedIds = [1, 3]

const allBoats = [
    {
        id: 1,
        name: 'titanic'
    },
    {
        id: 2,
        name: 'anna'
    },
    {
        id: 3,
        name: 'boaty McBoatface'
    }
]

const expectedResult = allBoats.reduce((p, c) => allowedIds.includes(c.id) ? p.concat(c.name) : p, []).sort((a, b) => a.localeCompare(b));

console.log(expectedResult);

Comments

0

if you want only those with id 1 or 3:

const selections = allBoats.filter((val)=>{return val.id === 1 || val.id ===3});
const result = selections.map((val,key)=>{ val.name}).sort()
console.log(result) //gives ["boaty McBoatface", "titanic"]

1 Comment

This will return the entire object, not just the name string, and is also significantly less flexible/maintainable than storing the ids in an array.

Your Answer

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