1

I have a list of objects that say look something like this:

const fruit = [
     {
          name: 'banana',
          id: 1
     },
     {
          name: 'apple',
          id: 8
     },
     {
          name: 'orange',
          id: 3
     }
];

And I have a list of Ids:

const ids = [8, 1];

And I'm looking to grab all the "fruit" that have an id in my list of ids, i.e. I want my result to look like:

const result = [
     {
          name: 'apple',
          id: 8
     },
     {
          name: 'banana',
          id: 1
     }
]

I have a "working" solution where I basically go:

let result = [];
for (let i = 0; i < ids.length; i += 1) {
     const foundFruit = fruit.find(x => x.id === ids[i]);
     result.push(foundFruit);
}

But ultimately I'm wondering if this is the most efficient way to do this, as in my real example, my list of fruit can potentially be thousands of fruit long, and I could similarly ALSO have a huge list of ids.

Would this approach be the best or is there a more efficient way of mapping these results?

1
  • 1
    sure, just do fruitMap = new Map(fruit.map(f => [f.id, f])) and then result = ids.map(id => fruitMap.get(id)) Commented Mar 18, 2021 at 20:48

3 Answers 3

1

You could collect all fruits by their id and map the collected ids.

const
    fruits = [{ name: 'banana', id: 1 }, { name: 'apple', id: 8 }, { name: 'orange', id: 3 }],
    ids = [8, 1],
    fruitsById = {};

for (const fruit of fruits) fruitsById[fruit.id] = fruit;

const result = ids.map(id => fruitsById[id]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

I assume the id property in the objects are unique, if that's the case then the following solution is better one.

I transformed the array of objects to an object with the keys of the id properties so the lookup for the ids becomes efficient.

const fruit = [
     {
          name: 'banana',
          id: 1
     },
     {
          name: 'apple',
          id: 8
     },
     {
          name: 'orange',
          id: 3
     }
];

var fruitsObj = {};

fruit.forEach(function (o) {
  fruitsObj[o.id] = o.name;
});

const ids = [8, 1];

var result = [];

ids.forEach(function (x){
  if (fruitsObj[x]) {
    result.push({name: fruitsObj[x], id: x});
  }
});

console.log(result);

Comments

0

you can do it in a more performant way by "hashing" the ids then only filtering them by lookup, like so:

const fruit = [
     {
          name: 'banana',
          id: 1
     },
     {
          name: 'apple',
          id: 8
     },
     {
          name: 'orange',
          id: 3
     }
];

const ids = [8, 1];

const result = fruit.filter(function (value) {
// return this.delete(value.id); // or use this return for unique ids
  return this.has(value.id);
}, new Set(ids));

console.log(result);

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.