Skip to main content
added 20 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Improving performance of formatting Formatting data for use in a controller?

I am using the function inputSetupinputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories$scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. Formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. Concatenating the arrays.

  3. Sorting the arrays.

  4. Using a for loop to iterate through the array and removing any duplicates that have the same "category" key... then removing the one that has "ticked" = false.

This code feels like it can be improved.

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

  1. Should the .concat and .sort be chained together?
  2. Should the for loop be replaced with an array.filter()?
$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

Improving performance of formatting data for use in controller?

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. Formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. Concatenating the arrays.

  3. Sorting the arrays.

  4. Using a for loop to iterate through the array and removing any duplicates that have the same "category" key... then removing the one that has "ticked" = false.

This code feels like it can be improved.

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

Formatting data for use in a controller

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. Formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. Concatenating the arrays.

  3. Sorting the arrays.

  4. Using a for loop to iterate through the array and removing any duplicates that have the same "category" key... then removing the one that has "ticked" = false.

This code feels like it can be improved.

  1. Should the .concat and .sort be chained together?
  2. Should the for loop be replaced with an array.filter()?
$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};
Fixed a couple of typos
Source Link

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. I am formattingFormatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. I am concatenatingConcatenating the arrays.

  3. I am sortingSorting the arrays.

  4. Using a for loop to iterate through the array and removeremoving any duplicates that have the same "category" key... thethen removing the one that has "ticked" = false.

This code feels like it can be improved.

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. I am formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. I am concatenating the arrays.

  3. I am sorting the arrays.

  4. Using a for loop to iterate through the array and remove any duplicates that have the same "category" key... the removing the one that has "ticked" = false.

This code feels like it can be improved.

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. Formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. Concatenating the arrays.

  3. Sorting the arrays.

  4. Using a for loop to iterate through the array and removing any duplicates that have the same "category" key... then removing the one that has "ticked" = false.

This code feels like it can be improved.

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};
Post Reopened by jacwah, Mast, Mathieu Guindon
Updated question to better explain what I am looking to accomplish
Source Link

Improvising Code Into A More DRY Approach Improving performance of formatting data for use in controller?

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. I am formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. I am concatenating the arrays.

  3. I am sorting the arrays.

  4. Using a for loop to iterate through the array and remove any duplicates that have the same "category" key... the removing the one that has "ticked" = false.

This code feels like it can be drastically improved. 

Should the for.concat and .sort be chained together?
Should the for loop be replaced with an array.filter()array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputCategoriesinputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

Improvising Code Into A More DRY Approach?

This code feels like it can be drastically improved. Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputCategories (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};

Improving performance of formatting data for use in controller?

I am using the function inputSetup to prepare an array for a multi-select directive. I am passing in an array with the existing selections ($scope.item.categories), an array with all the available categories, and a string the carries the context of the values "category".

  1. I am formatting the array to add the value "ticked" = true; on each object, so that the multi-select understand which options are selected.

  2. I am concatenating the arrays.

  3. I am sorting the arrays.

  4. Using a for loop to iterate through the array and remove any duplicates that have the same "category" key... the removing the one that has "ticked" = false.

This code feels like it can be improved. 

Should the .concat and .sort be chained together?
Should the for loop be replaced with an array.filter()?

$scope.categories = inputCategories($scope.item.categories, $scope.settings.categories, "category");

function inputSet (input, settings, category) {
  var updatedSettings = [];

  angular.forEach(input, function(obj) {
    var setting = { "ticked": true };
    setting[category] = obj;
    updatedSettings.push(setting);
  });

  var list = updatedSettings.concat(settings);

  list.sort(function(a, b) {
    return (a[category] > b[category]) - (a[category] < b[category]);
  });

  for ( var i = 1; i < list.length; i++ ){
    if(list[i-1][category] == list[i][category]) {
      list.splice(i,1);
    }
  }
  return list;
};
added 2 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Updated code with known ways to improve.
Source Link
Loading
Post Closed as "Not suitable for this site" by Mast, Quill, nhgrif, jacwah, Joseph
Source Link
Loading