11

I recently converted some code that made use of regular objects as maps to the new es6 Map class. I encountered an issue pretty quickly as, while the Map class includes a forEach like Array, it does not include a some method along with many other Array.prototype methods.

To give some context, the original code with regular JS objects looked something like this:

var map = {
    entry1: 'test',
    entry2: 'test2'
};

Object.keys(map).some(key => {
    var value = map[key];
    // Do something...found a match
    return true;
});

The Map class does include an entries method but sadly this returns an Iterator object. This doesn't include any easy way to access the Array.prototype methods either.

I'm curious if there's a clean way to do this or if I'm barking up the wrong tree.

2

2 Answers 2

10

Use the Map#values to get an iterator of the values, and the spread syntax or Array#from (a question of style) to convert the iterator to an array:

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);

const result = [...map.values()].some((value) => value > 2);

console.log(result);

As noted in @Paulpro comment you can use the same method to iterate Map#entries, and Map#keys. For example, using Array#reduce to convert the Map to an object. As Array#from invokes Map#entries we don't need to call it explicitly:

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);

const result = Array.from(map.entries()).reduce((obj, [key, value]) => {
  obj[key] = value;
  return obj;
}, {});

console.log(result);

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

6 Comments

+1, Also, if you want to use the key in the test use map.keys() or if you want both the key and value use map.entries() and use ([key,value]) for the arrow function's signature.
True, an es6 way. Except it doesn't seem like it's any better than what the OP is doing
Don't use spread syntax for type conversion, use the more explicit Array.from. Use spread syntax only when you need to construct an array of multiple elements.
@Bergi - please elaborate.
|
4

Call Array.from upon the Map object and invoke some on that:

Array.from(map).some(([key, value]) => /* something */ true)

Of course that's horribly inefficient. A much better idea is to define a some function that does work on any iterator, such as the ones that Map provides:

function some(it, pred) {
    for (const v of it)
        if (pred(v))
            return true;
    return false;
}

some(map.values(), value => /* something */ true)

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.