skip to Main Content

I have two objects below,

let response = {
                offer: {
                    custom_fields: {
                        job_title: 'engineer'
                    },
                    starts_at: 'test',
                    job_post: 'test'
                }
            }

let defaultFieldMapping = {
                "$.engagementData.title": {
                    default: "Software Engineer",
                    path: "$.offer.custom_fields.job_title",
                    fieldName: 'Title'
                },
                "$.engagementData.startDateUTC": {
                    default: null,
                    path: "$.offer.starts_at",
                },
            }

Need to form new object and expected result like below

let expectedResult = [
                {
                    externalFieldId: '$.offer.custom_fields.job_title',
                    integrationFieldId: '$.engagementData.title'
                },
                {
                    externalFieldId: '$.offer.starts_at',
                    integrationFieldId: '$.engagementData.startDateUTC'
                },
                {
                    externalFieldId: '$.offer.job_post',
                    integrationFieldId: ''
                },
            ]

integrationFieldId is empty string because that’s not exists in the defaultFieldMapping path column.

2

Answers


  1. Your code does not include any type information and is therefore just Java Script which is underlying to to Type Script. From your example it is not clear where the third entry is coming from but for the first two, you could simply write a function that returns the new object:

    const convertDefaultFieldMappingToExpectedResult = (defaultFileMapping) => {
        const expectedResult = []
        for (const [key, value] of Object.entries(defaultFileMapping)) {
            expectedResult.push(
                {
                    externalFieldId: value.path,
                    integrationFieldId: key
                }
            );
            return expectedResult;
        }
    }
    

    (Disclaimer: This code is untested.)

    Login or Signup to reply.
  2. function getDeepKeys(obj) {
      function* _getDeepKeys(obj, prefix) {
        for (let [k, v] of Object.entries(obj)) {
          if (typeof v === 'object' && v) yield* _getDeepKeys(v, `${prefix}${k}.`)
          else yield `${prefix}${k}`
        }
      }
      return [..._getDeepKeys(obj, '$.')];
    }
    
    console.clear()
    console.log(getDeepKeys(response))
    console.log(
      getDeepKeys(response)
        .map(externalFieldId => ({
          externalFieldId,
          integrationFieldId: Object.entries(defaultFieldMapping).find(([k, v]) => v.path === externalFieldId)?.[0]
        }))
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search