0
function a() {
     console.log('a');
}

function b() {
     console.log('b');
}

function c() {
     console.log('c');
}

let obj1 = {a:1}
let obj2 = {a:1, c:1}
let obj3 = {b:1, c:2}

Is this possible, write some code to execute function according to obj1 obj2 obj3 key?

example1 obj1 then execute function a()

example2 obj2 then execeute function a() and function c()

example3 obj3 then execute function b() and function c()

I saw some package always create new object and pass an object in second parameter.

ex: let test = new B('test', { a: 10, b: 20 }); ( B is a class )

It seen like execute something base on object key.

Sheri
  • 1,242
  • 2
  • 6
  • 21
TTT
  • 53
  • 7

1 Answers1

1

Your best bet here (barring more context that might suggest a completely different approach) is to put those functions in an object or Map, and then use the property name as a key into that object or map to make the call:

const functions = {
    a() {
        console.log('a');
    },
    b() {
        console.log('b');
    },
    c() {
        console.log('c');
    }
};

let obj1 = {a:1};
let obj2 = {a:1, c:1};
let obj3 = {b:1, c:2};

function callForObj(label, obj) {
    console.log(label);
    for (const key of Object.keys(obj)) {
        functions[key]();
    }
}

callForObj("obj1:", obj1);
callForObj("obj2:", obj2);
callForObj("obj3:", obj3);

That uses Object.keys and is based purely on the object having a property with that key (ignoring the value). If you want to take values into account, use Object.entries instead, perhaps with destructuring:

function callForObj(label, obj) {
    console.log(label);
    for (const [key, value] of Object.entries(obj)) {
        if (value) {
            functions[key]();
        }
    }
}

const functions = {
    a() {
        console.log('a');
    },
    b() {
        console.log('b');
    },
    c() {
        console.log('c');
    }
};

let obj1 = {a:1};
let obj2 = {a:1, c:1};
let obj3 = {b:1, c:2};

function callForObj(label, obj) {
    console.log(label);
    for (const [key, value] of Object.entries(obj)) {
        if (value) {
            functions[key]();
        }
    }
}

callForObj("obj1:", obj1);
callForObj("obj2:", obj2);
callForObj("obj3:", obj3);

Note: If the order of the calls is important, I'd suggest using an array rather than an object. Even though ES2015 adds order to object properties, and ES2020 requires that Object.keys and entries and for-in follow it (which ES2015 didn't), it's still fragile because for non-index-named properties, it depends on the order in which they were created:

console.log(Object.keys({a: 1, b: 2})); // ["a", "b"]
console.log(Object.keys({b: 2, a: 1})); // ["b", "a"]
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • But this doesn't guarantee that `a` is called before `c`, because objects don't guarantee order, do they? – schroffl Mar 24 '20 at 15:06
  • I think I misunderstood the question. It seems the order does not matter. – schroffl Mar 24 '20 at 15:08
  • @schroffl - Ah, that's a good point, I didn't really twig to order being important in the question (I can read it both ways). Objects **do** have order now (as of ES2015), even for older operations like `Object.keys` (as of ES2020), so in a modern JavaScript engine the above **is** guaranteed to call `a` before `c`. But using that order is rarely useful or appropriate; better to use an array if order is important. – T.J. Crowder Mar 24 '20 at 15:08
  • @schroffl [refer to this question](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). For questions on property order, there is hardly a thing that's not worth reading in there. – ASDFGerte Mar 24 '20 at 15:08
  • @ASDFGerte - Oriol's answer to that question is out of date now. (Depending on how pedantic you want to be, since the question is "Does **ES6** introduce..." :-) ES2020 makes it apply even to `Object.keys` and such.) – T.J. Crowder Mar 24 '20 at 15:09
  • Yes, but CertainPerformance posted just that as an answer aswell. On top of that, trincot mentions a mistake in the previous engine realizations (does he? Maybe he forgot to link, this is [the related post](https://stackoverflow.com/questions/60121521/object-keys-order-for-large-numerical-indexes)), along with a recent spec change, which is not really too relevant for normal programming, but still an interesting side fact. Luckily, for that question, there is almost no spam at all. – ASDFGerte Mar 24 '20 at 15:11