JSFiddle - React, Tailwind, and code Playground

by dshilkret

JavaScript

function transformStringInput(inputStr, templateObject) {
    var resultObject = Object.assign({}, templateObject);
    var conditions = inputStr.split(' and ');
    
    conditions.forEach(function(condition) {
        // Split the condition by ' eq ' to separate the key and value
        var parts = condition.split(' eq ');
        var cleanKey = parts[0].trim();

        // Add the key with an empty string value if the key is not already in the templateObject
        if (!(cleanKey in templateObject)) {
            resultObject[cleanKey] = "";
        }
    });

    // Return the modified result object
    return resultObject;
}

// Example input string
var inputStr = "template_number eq 'INC0000008' and Task_ID eq 'INC0000008'";

// Example template object
var templateObject = {
    "template_number": "INC0000009",
    "__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 input string
var transformedObject = transformStringInput(inputStr, templateObject);
console.log(transformedObject);