1

How to map values of one object to other if keys matched, trying to assign values here additionalData.value =userParams[prop] ? userParams[prop] : ''; however it pushing empty string any idea what has implemented wrong here ? or any better approach to achieve this task

const getParams = (userParams) => {
    const interactionData = [];
    const interactionDataDict  = {
        firstName: [{key: "FIRST_NAME", defaultValue: null}],
        lastName: [{key: "LAST_NAME", defaultValue: null}],
        SourceSystem: [{key: "SOURCE_SYSTEM", defaultValue: null}]
    };
    for (const prop in interactionDataDict) {
        if (prop) {
            for (const val of interactionDataDict[prop]) {
                const additionalData = {};
                additionalData.key = val.key;
                if (val.defaultValue) {
                    additionalData.value = val.defaultValue;
                }
                else {
                    additionalData.value =
                        userParams[prop] ? userParams[prop] : '';
                }
                interactionData.push(additionalData);
            }
        }
    }
    return interactionData;
}

const args = { 
    firstName: "John",
    lastName: "Hayne",
    sourceSystem: "HTS"
}

console.log(getParams(args))

expected output

[{"key":"FIRST_NAME","value":"John"},{"key":"LAST_NAME","value":"Hayne"},{"key":"SOURCE_SYSTEM","value":"HTS"}]
5
  • why has interactionDataDict arrays? Commented Sep 24, 2020 at 21:15
  • It looks as though there's a typo in interationDataDict. Why is the key SourceSystem capitalized and the others are not. If you change, this, I think your problem goes away. Commented Sep 24, 2020 at 21:16
  • to have pre assignment for the default value as null i had to create interationDataDict as an array Commented Sep 24, 2020 at 21:18
  • @hussain: The point is why is interactionDataDict.firstName the single-element array [{key: "FIRST_NAME", defaultValue: null}] instead of the plain object {key: "FIRST_NAME", defaultValue: null}? Commented Sep 24, 2020 at 21:24
  • never thought of this it may be easier with plain object Commented Sep 24, 2020 at 21:27

2 Answers 2

1

You could map the entries and get key and value for the new objects.

function getParams(userParams) {
    const interactionDataDict = { firstName: [{ key: "FIRST_NAME", defaultValue: null }], lastName: [{ key: "LAST_NAME", defaultValue: null }], SourceSystem: [{ key: "SOURCE_SYSTEM", defaultValue: null }] }; 
    return Object
        .entries(interactionDataDict)
        .map(([k, [{ key, defaultValue }]]) =>
            ({ key, value: userParams[k] || defaultValue })
        );
}

const args = { firstName: "John", lastName: "Hayne", sourceSystem: "HTS" }

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

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

6 Comments

Can you simplify this it is throwing alot of errors with es6 code i guess
@hussain: what environment are you running in that ES6 doesn't work?
@ScottSauyet gettign this error Type 'unknown' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488
@Nina: Note, though, that if interactionDataDict is really local to this function (instead of an additional parameter or something in a higher scope), you might as well store it as the result of that Object.entries call, something like [['firstName', {key: ... defaultValue: ...}], ['lastName', {...}], ...], and then have the function consist only of the map call.
@ScottSauyet that might be the issue interactionDataDict is not local to this function it is private attribute of the class , i added in the question in local scope to better understand instead of adding class code but even after changing to this. interactionDataDict it didn't work
|
0

I wrote an answer independently, but ended up with the same logical solution as Nina. But I structured it somewhat differently, and it might be of interest:

const buildDefaults = (dict) => (args) => 
  Object 
    .entries (dict)
    .map (([k, [{key, defaultValue}]]) => ({key, value: args [k] || defaultValue}))


const getParams = buildDefaults ({
  firstName: [{key: "FIRST_NAME", defaultValue: null}],
  lastName: [{key: "LAST_NAME", defaultValue: null}],
  sourceSystem: [{key: "SOURCE_SYSTEM", defaultValue: null}]
})

const args = { 
  firstName: "John",
  lastName: "Hayne",
  sourceSystem: "HTS"
}

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

Here we write the generic function buildDefaults that takes the dictionary and returns a function that takes args and fills in your output the way you want. Then getParams is created by calling it with your dictionary.

If, as discussed in the comments, you wanted to remove the array wrappers in the dictionary, so that it looks like this:

{
    firstName: {key: "FIRST_NAME", defaultValue: null},
    lastName: {key: "LAST_NAME", defaultValue: null},
    sourceSystem: {key: "SOURCE_SYSTEM", defaultValue: null}
}

then you could just replace it in the destructured parameter by replacing this line:

    .map (([k, [{key, defaultValue}]]) => ({key, value: args [k] || defaultValue}))

with this:

    .map (([k, {key, defaultValue}]) => ({key, value: args [k] || defaultValue}))

2 Comments

i think its just this line if you can help how do i map userParams instead of writing complex code additionalData.value = eci[prop] ? eci[prop] : userParams[prop] ? userParams[prop] : ''; in my orginal code
@hussain: I don't know what that is. eci? My answer and Nina's answer both seem to do what you need. But my first comment on the question shows the smallest change to get this working. I find either of the answers much simpler than your code, so we have very different ideas about "complex code". Sorry.

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.