简体   繁体   English

希望将这些连接成一个带连字符的字符串

[英]looking to concatenate these into a single string with hyphen

I am having mapping function like this below where i am assigning this object to searchable drop down list in other page我在下面有这样的映射功能,我将此对象分配给其他页面中的可搜索下拉列表

  const mapOptions =
equipmentDensitySourceTypeData !== (undefined && null)
  ? equipmentDensitySourceTypeData.libraryEquipment.map(code => ({
      label:
        code.equipmentSource.name -
        code.equipmentSource.edition -
        code.category -
        code.spaceFunction -
        code.revision,
      key: code.id,
    }))
  : null;

here only the revision is integer and remaining all are strings when i check mapOptions object i am getting label value as NaN and for key i am getting correct value ..这里只有修订版是整数,其余的都是字符串,当我检查mapOptions对象时,我获得的label值为NaN而对于键,我获得了正确的值..

I am looking for label should be string separated by -我正在寻找的标签应该是由字符串分隔的-

I am not sure where i am doing wrong with this one, Could any please suggest any idea on this.. I am using React JS with ES6 for the above code我不确定我在这方面做错了什么,有没有人可以对此提出任何建议..我正在将 React JS 与 ES6 用于上述代码

PS : I also tried like this ${code.category} - ${code.spaceFunction} but getting same NaN PS:我也试过这样${code.category} - ${code.spaceFunction}但得到相同的NaN

You can use template literals to concate the string.您可以使用模板文字来连接字符串。 Note the usage of back tick注意反勾的用法

const mapOptions =
  equipmentDensitySourceTypeData !== (undefined && null) ?
  equipmentDensitySourceTypeData.libraryEquipment.map(code => ({
    label: `${code.equipmentSource.name}-
            ${code.equipmentSource.edition}-
            ${code.category}-
            ${code.spaceFunction}-
            ${code.revision}`,
    key: code.id,
  })) :
  null;

Try using -尝试使用 -

const mapOptions =
equipmentDensitySourceTypeData && 
equipmentDensitySourceTypeData.libraryEquipment.map(code => ({
  label:
    `${code.equipmentSource.name} -
    ${code.equipmentSource.edition} -
    ${code.category} -
    ${code.spaceFunction} -
    ${code.revision}`,
  key: code.id,
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM