-1

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 ..

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

PS : I also tried like this ${code.category} - ${code.spaceFunction} but getting same NaN

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • Does this answer your question? [What's the best way to do string building/concatenation in JavaScript?](https://stackoverflow.com/questions/31845895/whats-the-best-way-to-do-string-building-concatenation-in-javascript) – Emile Bergeron Feb 21 '20 at 03:45

2 Answers2

3

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;
brk
  • 48,835
  • 10
  • 56
  • 78
1

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,
}));