Node.js对象中的字符串操作 [英] String operation in nodejs Objects

查看:269
本文介绍了Node.js对象中的字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodeJS中有一个场景。



我有一个包含元素数量和数组的对象

  var Obj = {
计数:3,
项:[{
organizationCode: FP1,
organizationName: FTE流程组织
},
{
organizationCode: T11,
organizationName: FTE离散组织
},
{
organizationCode: M1,
organizationName:西雅图制造业
}
]
};

这种情况是这样的。我必须根据条件过滤结果。
如果 organizationCode organizationName 特定字符或结尾,或者包含特定单词。
例如,如果用户在下面输入以M开头的或以 M开头的应该返回

  {
organizationCode: M1,
organizationName:西雅图制造
}

如果用户输入的结尾是org,则



< pre class = lang-js prettyprint-override> {
organizationCode: FP1,
organizationName: FTE Process Org
}, {
organizationCode: T11,
organizationName: FTE Discrete Org
},

,如果输入包含11 ,则应返回以下内容。

  {
organizationCode: T11,
organizationName: FTE Discrete Org
},

我有成千上万的记录。我正在寻找获取输出的优化方法。我是 NodeJs 的新手,所以很难完成它。

解决方案

第一个首先。您可以在此处使用一些字符串函数:



endsWith
一些字符串。endsWith('string')



startsWith
某些字符串 .startsWith( 'some')



包括
一些字符串。includes('ome')



您可以使用Object.values()来交换对象。



但是如果我这样做了,我可能会想到以下内容,

  //我假设在nodejs中,您将需要一些变量来搜索
//如whereToSearch而whatToSearch可以说

var arrToSearch = [
{OrganizationCode: FP1,organizationName: FTE Process Org} ,
{OrganizationCode: T11,organizationName: FTE Discrete Org},
{OrganizationCode: M1,organizationName: Seattle Manufacturing},
];

函数搜索(arr,whereToSearch,whatToSearch){
返回arr.filter(
(ch)=>
ch.organizationCode [whereToSearch](whatToSearch) ||
ch.organizationName [whereToSearch](whatToSearch)
);
}

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

//我们也可以将数组发送给函数,因此在这种情况下,您的对象在这里
var object = {
count:3,
items:[
{
机构代码: FP1,
机构名称: FTE流程组织,
},
{
机构代码: T11,
组织名称: FTE离散组织,
},
{
组织代码: M1,
组织名称:西雅图制造,
},
],
};
// //您可以使用

console.log(search(object.items, startsWith, FTE));


I have a scenario in nodeJS.

I have an object which contains number of elements and array

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

The scenario is like this. I have to filter result based on criteria. I have print output if either organizationCode or organizationName starts with particular character or ends with or it contains particular word. for eg if user enters starts with M or starts with "M" below should return

{
  "organizationCode": "M1",
  "organizationName": "Seattle Manufacturing"
}

if user enters ends with org then

{
  "organizationCode": "FP1",
  "organizationName": "FTE Process Org"
}, {
  "organizationCode": "T11",
  "organizationName": "FTE Discrete Org"
},

and if it enters contains 11 then below should return.

{
  "organizationCode": "T11",
  "organizationName": "FTE Discrete Org"
},

I have thousands of record. I am looking for optimize way to get output. I am new to NodeJs so struggling to complete it.

解决方案

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 say

var arrToSearch = [
  { organizationCode: "FP1", organizationName: "FTE Process Org" },
  { organizationCode: "T11", organizationName: "FTE Discrete Org" },
  { organizationCode: "M1", organizationName: "Seattle Manufacturing" },
];

function search(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 here
var 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 is

console.log(search(object.items, "startsWith", "FTE"));

这篇关于Node.js对象中的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆