2

I have an object that is build with from an html form. When i fill the form I obtain this json object we call "jsonObjectForm" :

{
  "inputID1": "content1",
  "inputID2": "content2",
  "inputID3": "content3",
  "beginningDate": "2020-02-02",
  "endingDate": "2022-03-03",
  "inputID4": "content4",
  "inputID5": "content5",
  "inputID6": "content6",
  "inputID7": "content7",
  "inputID8": "content8"
}

I have a second object that i have to use to build a sql query. this object is

let fieldNames = {
    "inputID1": "dbField1",
    "inputID2": "dbField2",
    "inputID3": "dbField3",
    "inputID4": "dbField4",
    "inputID5": "dbField5",
    "inputID6": "dbField6",
    "inputID7": "dbField7",
    "inputID8": "dbField7",
};

I found the way to browse the first object. I defined the form elements i want to "scan" in the form :

const pageFormElements = getAllFormElements(document.getElementById("requestForm"));

for(let [key, value] of Object.entries(jsonObjectForm)) {
    pageFormElements.forEach(element => {
        console.log("key: " + key + " | value: " + value);
    }
}

My question is how do i manage the double forEach in this case and I wonder if there's a way to "map" the values from the first object to the second and build a final object that would look like this:

let fieldNames = {
    "dbField1": "content1",
    "dbField2": "content2",
    "dbField3": "content3",
    "dbField4": "content4",
    "dbField5": "content5",
    "dbField6": "content6",
    "dbField7": "content7",
    "dbField7": "content8",
};

My objective is to create a query like this

query = select * from whatever where dbField1 = content1 AND dbField2 = content2 ... AND dbField8 = content8;
6
  • do you have empty values? what happens with numbers? or substrings? Commented Nov 15, 2019 at 10:08
  • Yes i can have empty values. And the second object do not include the date that are managed separatelly... Commented Nov 15, 2019 at 10:10
  • why not change the input fields of the form dircty to the names of the database field names? Commented Nov 15, 2019 at 10:12
  • I can try this way but i don't think it will be appreciated ... Commented Nov 15, 2019 at 10:14
  • what should happen with the date? which field is associated to it? Commented Nov 15, 2019 at 10:18

3 Answers 3

2

You could iterate the fields and assemble the wanted query by having a look to keys with beginning and ending prefix for BETWEEN expressions.

var data = { inputID1: "content1", inputID2: "content2", inputID3: "content3", beginningDate: "2020-02-02", endingDate: "2022-03-03", inputID4: "content4", inputID5: "content5", inputID6: "content6", inputID7: "content7", inputID8: "content8" },
    fields = { Date: 'date', inputID1: "dbField1", inputID2: "dbField2", inputID3: "dbField3", inputID4: "dbField4", inputID5: "dbField5", inputID6: "dbField6", inputID7: "dbField7", inputID8: "dbField7" },
    result = Object
        .entries(fields)
        .map(([k, v]) => (('beginning' + k) in data) && (('ending' + k) in data)
            ? `${v} BETWEEN '${data['beginning' + k]}' AND '${data['ending' + k]}'`
            : `${v}='${data[k]}'`
        )
        .join(' AND ');

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

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

4 Comments

this works perfectly thanks ! i should be able from there to manage the "undefined" when field is not set..
You could filter first before mapping, or take flatMap and insert an empty array.
I used a regex and replace what i want to remove :) i'll have a look at flatmap ! thanks
here is the regex i used : (?:[a-zA-Z0-9_]+='undefined' <[/]And>)
2

You can use something like this:

const content = {
  "inputID1": "content1",
  "inputID2": "content2",
  "inputID3": "content3",
  "beginningDate": "2020-02-02",
  "endingDate": "2022-03-03",
  "inputID4": "content4",
  "inputID5": "content5",
  "inputID6": "content6",
  "inputID7": "content7",
  "inputID8": "content8"
}

const fieldNames = {
  "inputID1": "dbField1",
  "inputID2": "dbField2",
  "inputID3": "dbField3",
  "inputID4": "dbField4",
  "inputID5": "dbField5",
  "inputID6": "dbField6",
  "inputID7": "dbField7",
  "inputID8": "dbField7",
};

const result = Object.entries(fieldNames).reduce((a, [k, v]) => (a[v] = content[k], a), {});

console.log(result);

This will go through fieldNames entries and generate a new object using the entry's value as key and as value, the value from the content object that matches the entry's key.

Comments

1

This code can do it for you:

const content = {
  "inputID1": "content1",
  "inputID2": "content2",
  "inputID3": "content3",
  "beginningDate": "2020-02-02",
  "endingDate": "2022-03-03",
  "inputID4": "content4",
  "inputID5": "content5",
  "inputID6": "content6",
  "inputID7": "content7",
  "inputID8": "content8"
}

const fieldNames = {
  "inputID1": "dbField1",
  "inputID2": "dbField2",
  "inputID3": "dbField3",
  "inputID4": "dbField4",
  "inputID5": "dbField5",
  "inputID6": "dbField6",
  "inputID7": "dbField7",
  "inputID8": "dbField7",
};
const result = {};
Object.keys(fieldNames).forEach(key => result[fieldNames[key]] = content[key]);
console.log(result);

Comments

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.