Snips & Tips
Snips & Tips
  • Snips & Tips
  • 📊Data Science
    • Polars Dataframe Library
    • Loading large data
    • Pandas
      • Pandas Apply Function
    • Apache Spark
      • Custom Transformer
    • Data Visualizations
    • Jupyter Notebooks
      • Jupyter Notebook Structure
    • Probability
    • Statistics
      • Statistical Tests
      • Z - Test
      • Hypothesis Testing
    • SQL
      • SQL Tips
      • Creating new columns
  • ☘️Deep Learning
    • Backpropagation in Deep Learning
    • Pytorch Early Stopping
    • Optimizers
  • Pytorch Tensor Shapes
  • 🔖Machine Learning
    • Handling Imbalanced Dataset
    • Time Series Forecasting
      • Hierarchical Time Series Forecasting
      • Facebook Prophet
      • Misc
    • Handling high dimensionality data
      • Weight of evidence and Information value
    • Debugging ML Models
    • Feature Engineering
      • Time Series
      • Outlier Detection
      • Categorical Encoding
      • Feature Scaling
  • 🐲DSA
    • Arrays
  • 🖥️WEB DEV
    • Typescript
    • React State Management
    • Redux Boilerplate
    • Intercept a HTTP request or response
    • this keyword
    • Array Methods
    • Throttle Debounce
    • Media Queries
    • React Typeahead Search
  • Replace text with React Component
  • 💻Product Analytics
    • Product Sense
    • Customer Segmentation
  • 🖥️Terminal
    • Terminal Commands
    • Jupyter Notebook 2 HTML
  • 🪛Tools and Libraries
    • Web Based
    • Databases
  • 🚟Backend
    • Fast API CRUD
    • Scalable APIs
  • 💸Quant Finance
    • Misc
    • Factor Investing
  • 🎮Game Dev
    • Misc
  • 🛠️Architecture
    • Docker
    • AWS CDK
  • 🦠Artificial Intelligence
    • AI Engg
Powered by GitBook
On this page
  • Array.reduce for object
  • Array.sort for object
  • Slice and Splice
  • Slice
  • Splice

Was this helpful?

Edit on GitHub
  1. WEB DEV

Array Methods

Array.reduce for object

var obj = [
  {
    id: 5,
    count: 7,
  },
  {
    id: 3,
    count: 45,
  },
  {
    id: 8,
    count: 35,
  },
  {
    id: 1,
    count: 15,
  },
];

let res = obj.reduce(
  (acc, next) => {
    return {
      id: acc.id + next.id,
      count: acc.count + next.count,
    };
  },
  {
    id: 0,
    count: 0,
  },
);

console.log(res); // {count: 102,id: 17}

Array.sort for object

var obj = [
  {
    id: 5,
    count: 7,
  },
  {
    id: 3,
    count: 45,
  },
  {
    id: 8,
    count: 35,
  },
  {
    id: 1,
    count: 15,
  },
];

let sorted = obj.sort((a, b) => {
  return a.id - b.id;
});

Slice and Splice

Slice

Think of "slicing" as cutting a portion out of an array without modifying the original array. slice returns a shallow copy of a portion of an array, specified by a start and end index.

const originalArray = [1, 2, 3, 4, 5];
const newArray = originalArray.slice(1, 4); // Returns [2, 3, 4]

Splice

Think of "splicing" as modifying or changing the original array. splice is used to add or remove elements from an array at a specified index.

const originalArray = [1, 2, 3, 4, 5];
originalArray.splice(2, 1); // Removes 1 element at index 2
// originalArray is now [1, 2, 4, 5]

Remember the keyword associations: "slice" for creating a new sliced array, "splice" for modifying the original array by splicing in or out elements.

In summary:

  • slice does not modify the original array and returns a new array.

  • splice modifies the original array by adding or removing elements.

Previousthis keywordNextThrottle Debounce

Last updated 1 year ago

Was this helpful?

🖥️