Skip to content Skip to sidebar Skip to footer

String Operation In Nodejs Objects

I have a scenario in nodeJS. I have an object which contains number of elements and array var Obj = { count: 3, items: [{ 'organizationCode': 'FP1', 'organizationNa

Solution 1:

First thing first. here some string functions that u can use:

endsWith"some string".endsWith('string')

startsWith"some string".startsWith('some')

includes"some string".includes('ome')

And you can convers object with Object.values().

But if i was doing this, i would probably tho something like following,

// I'm assuming in the nodejs you would have some variables to search for// like whereToSearch and whatToSearch lets sayvar arrToSearch = [
  { organizationCode: "FP1", organizationName: "FTE Process Org" },
  { organizationCode: "T11", organizationName: "FTE Discrete Org" },
  { organizationCode: "M1", organizationName: "Seattle Manufacturing" },
];

functionsearch(arr, whereToSearch, whatToSearch) {
  return arr.filter(
    (ch) =>
      ch.organizationCode[whereToSearch](whatToSearch) ||
      ch.organizationName[whereToSearch](whatToSearch)
  );
}

console.log(search(arrToSearch, "endsWith", "11"));

// We can also send the array to function so in this case your object is herevar object = {
  count: 3,
  items: [
    {
      organizationCode: "FP1",
      organizationName: "FTE Process Org",
    },
    {
      organizationCode: "T11",
      organizationName: "FTE Discrete Org",
    },
    {
      organizationCode: "M1",
      organizationName: "Seattle Manufacturing",
    },
  ],
};
//and what you can do with this isconsole.log(search(object.items, "startsWith", "FTE"));

Solution 2:

You can use Array.filter in conjunction with String.startsWith, String.includes and String.endsWith.

Solution 3:

You can use an array filter, to go trough all of the elements and process them as you need.

Also to check if a string contains another string you can use an insensitive match on it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

varObj = {
	count: 3,
	items: [
		{
			organizationCode: "FP1",
			organizationName: "FTE Process Org",
		},
		{
			organizationCode: "T11",
			organizationName: "FTE Discrete Org",
		},
		{
			organizationCode: "M1",
			organizationName: "Seattle Manufacturing",
		},
	],
};

constfilter = (starts_with, ends_with, contains) => {
    const result = Obj.items.filter(item => {
        if(starts_with){
            return (
                item.organizationCode[0].toLowerCase() === starts_with.toLowerCase() ||
                item.organizationName[0].toLowerCase() === starts_with.toLowerCase()
            ) ? true : false
        } elseif(ends_with) {
            return (
                item.organizationCode[item.organizationCode.length].toLowerCase() === ends_with.toLowerCase() ||
                item.organizationName[item.organizationName.length].toLowerCase() === ends_with.toLowerCase()
            ) ? true : false
        } else {
            return (
                item.organizationCode.match(contains,'i') ||
                item.organizationName.match(contains,'i')
            ) ? true : false
        }
    });
    return result;
};

console.log(filter("F",null,null)); // test run

Solution 4:

Here is a simple solution analyzing initially the user condition and the filtering your list.

It has several improvement points but it is a good start.

constanalyzeCond = condition => {
  let conditionParts = condition.split(' ').filter(str => str !== '');
  if (conditionParts.length < 3) {
    returnnull;
  }
  // Check fieldconst fieldName = conditionParts.shift();

  if (!['organizationCode', 'organizationName'].includes(fieldName)) {
    returnnull;
  }

  // Check operatorconst operator = conditionParts.shift();
  if (['starts', 'ends'].includes(operator)) {
    const withWord = conditionParts.shift();
    if (withWord !== 'with') {
      returnnull;
    }
  } elseif (operator !== 'contains'){
    returnnull;
  }

  const userText = conditionParts.join(' ');

  return { fieldName, operator, userText };
};

const compareFns = {
  starts: (query, text) => text.startsWith(query),
  ends: (query, text) => text.endsWith(query),
  contains: (query, text) => text.includes(query),
};

constfilter = (list, userInput) => {
  const condition = analyzeCond(userInput);
  if (condition === null) {
    returnnull;
  }

  // Filter based on user inputreturn list.filter(item => {
    return compareFns[condition.operator](condition.userText, item[condition.fieldName]);
  });
};


const obj = {
  count: 3,
  items: [{
    "organizationCode": "FP1",
    "organizationName": "FTE Process Org"
  },
    {
      "organizationCode": "T11",
      "organizationName": "FTE Discrete Org"
    },
    {
      "organizationCode": "M1",
      "organizationName": "Seattle Manufacturing"
    }]
};

// Example execution with ugly but possible user inputconst result = filter(obj.items, '    organizationName      starts     with      Seattle Ma');
if (result !== null) {
  console.log('Filter result', result);
} else {
  console.log('The user input has not the expected format');
}

Things to check:

  • Check if you want to support multiple continued space characters or not (you can adapt this code if needed).

  • You could easily add more operators or fields to compare against.

  • If the user conditions you want to support in the future are more complex I'd recommend to use a parser package to not reinvent the well.

  • If you are in big project and want to implement complex searches, consider using services like ElasticSearch.

Post a Comment for "String Operation In Nodejs Objects"