Skip to main content
added 295 characters in body
Source Link
Tushar
  • 3k
  • 1
  • 22
  • 28

The code looks like you're passing the processed data to autocomplete.

Naming

  • formatToInput: This gives no idea of what the function is doing.
  • empty: Is it really empty at the end of formatToInput?
  • obj, list: Confusing.
  • test, results: Can be named better.

Lodash

Do you really need a library for this functionality? The Same functionality can be done without Lodash or any other library. If Lodash is only used for this functionality, it can be removed.

Missing Semi-colons

Although, a small thing Automatic Semicolon Insertion can lead to unexpected behavior. The usage of the semi-colon is not consistent. I'll recommend using JSLint/JSHint.

Alternative

I'll recommend the use of Array#map and String methods to make the first character uppercase.

const colorValueMap = {
  blue: 10,
  green: 5,
  yellow: 3,
  pink: 1
};
const colors = ['green', 'yellow'];

const result = colors.map(color => ({
  label: color[0].toUpperCase() + color.slice(1),
  value: colorValueMap[color]
}));
console.log(result);


Above solution assumes that the value from the filter array always exists in the object. If this is not the case, you can first filter the array and then use map on it.

colors.filter(color => colorValueMap.hasOwnProperty(color)).map(color => ({
    ...
});

The code looks like you're passing the processed data to autocomplete.

Naming

  • formatToInput: This gives no idea of what the function is doing.
  • empty: Is it really empty at the end of formatToInput?
  • obj, list: Confusing.
  • test, results: Can be named better.

Lodash

Do you really need a library for this functionality? The Same functionality can be done without Lodash or any other library. If Lodash is only used for this functionality, it can be removed.

Missing Semi-colons

Although, a small thing Automatic Semicolon Insertion can lead to unexpected behavior. The usage of the semi-colon is not consistent. I'll recommend using JSLint/JSHint.

Alternative

I'll recommend the use of Array#map and String methods to make the first character uppercase.

const colorValueMap = {
  blue: 10,
  green: 5,
  yellow: 3,
  pink: 1
};
const colors = ['green', 'yellow'];

const result = colors.map(color => ({
  label: color[0].toUpperCase() + color.slice(1),
  value: colorValueMap[color]
}));
console.log(result);

The code looks like you're passing the processed data to autocomplete.

Naming

  • formatToInput: This gives no idea of what the function is doing.
  • empty: Is it really empty at the end of formatToInput?
  • obj, list: Confusing.
  • test, results: Can be named better.

Lodash

Do you really need a library for this functionality? The Same functionality can be done without Lodash or any other library. If Lodash is only used for this functionality, it can be removed.

Missing Semi-colons

Although, a small thing Automatic Semicolon Insertion can lead to unexpected behavior. The usage of the semi-colon is not consistent. I'll recommend using JSLint/JSHint.

Alternative

I'll recommend the use of Array#map and String methods to make the first character uppercase.

const colorValueMap = {
  blue: 10,
  green: 5,
  yellow: 3,
  pink: 1
};
const colors = ['green', 'yellow'];

const result = colors.map(color => ({
  label: color[0].toUpperCase() + color.slice(1),
  value: colorValueMap[color]
}));
console.log(result);


Above solution assumes that the value from the filter array always exists in the object. If this is not the case, you can first filter the array and then use map on it.

colors.filter(color => colorValueMap.hasOwnProperty(color)).map(color => ({
    ...
});
Source Link
Tushar
  • 3k
  • 1
  • 22
  • 28

The code looks like you're passing the processed data to autocomplete.

Naming

  • formatToInput: This gives no idea of what the function is doing.
  • empty: Is it really empty at the end of formatToInput?
  • obj, list: Confusing.
  • test, results: Can be named better.

Lodash

Do you really need a library for this functionality? The Same functionality can be done without Lodash or any other library. If Lodash is only used for this functionality, it can be removed.

Missing Semi-colons

Although, a small thing Automatic Semicolon Insertion can lead to unexpected behavior. The usage of the semi-colon is not consistent. I'll recommend using JSLint/JSHint.

Alternative

I'll recommend the use of Array#map and String methods to make the first character uppercase.

const colorValueMap = {
  blue: 10,
  green: 5,
  yellow: 3,
  pink: 1
};
const colors = ['green', 'yellow'];

const result = colors.map(color => ({
  label: color[0].toUpperCase() + color.slice(1),
  value: colorValueMap[color]
}));
console.log(result);