JSFiddle - React, Tailwind, and code Playground

by dshilkret

JavaScript

function transformObject(input, templateObject) {
    // Create a copy of the template object to avoid mutating the original
    const resultObject = { ...templateObject };

    // Iterate over the input object keys
    for (const key in input) {
        if (input.hasOwnProperty(key) && key !== "__metadata") {
            // Set the template_number value
            resultObject.template_number = input[key];
        }
    }

    // Return the modified result object
    return resultObject;
}

// Example input object
const inputObject = {
    "__metadata": {
        "type": "SP.Data.Dev7Lib1Item"
    },
    "Task_ID": "INC0000009"
};

// Example template object
const templateObject = {
    "template_number": "",
    "Task_ID": "",
    "__metadata": {
        "type": "SP.Data.Dev7Lib1Item"
    },
    "Title": "200.AUS - Building Workflow Solutions with K2 Studio - Intermediate 3 1 1 7 1 1 1 1",
    "ContentTypeId": "0x01010083A32191057A064F946148C51234A1ED004D1552897679724CB83E4100BA5A2DE7"
};

// Transform the object
const transformedObject = transformObject(inputObject, templateObject);
console.log(transformedObject);