const result = {};
result.items = [{
"organizationCode": "FP1",
"organizationName": "FTE Process Org"
},
{
"organizationCode": "T11",
"organizationName": "FTE Discrete Org"
},
{
"organizationCode": "M1",
"organizationName": "Seattle Manufacturing"
}
];
let inputText = "starts with M"; // example input text
const lastSpaceIndex = inputText.lastIndexOf(' ');
const secondPartOfInput = inputText.substring(lastSpaceIndex + 1).trim();
const firstPartOfInput = inputText.substring(0, lastSpaceIndex).trim().toLowerCase();
const filtered = result.items.filter(item => {
if (firstPartOfInput === "starts with")
return item.organizationCode.startsWith(secondPartOfInput) || item.organizationName.startsWith(secondPartOfInput);
if (firstPartOfInput === "ends with")
return item.organizationCode.endsWith(secondPartOfInput) || item.organizationName.endsWith(secondPartOfInput);
if (firstPartOfInput === "contains")
return item.organizationCode.includes(secondPartOfInput) || item.organizationName.includes(secondPartOfInput);
return false;
})
console.log(filtered);
3
solved String operation in NodeJS