Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Data Transformation with Array Methods

- 17.08.26 - ErcanOPAK

🔄 Data Transformation = Clean Data

Raw data needs processing. JavaScript array methods transform, filter, and aggregate data into usable formats.

📝 Data Processing Patterns

// User Data
const users = [
    { id: 1, name: 'Alice', age: 25, city: 'NYC', active: true },
    { id: 2, name: 'Bob', age: 30, city: 'LA', active: false },
    { id: 3, name: 'Charlie', age: 35, city: 'NYC', active: true },
    { id: 4, name: 'Diana', age: 28, city: 'SF', active: true },
    { id: 5, name: 'Eve', age: 22, city: 'LA', active: false }
];

// 1. Filter Active Users
const activeUsers = users.filter(u => u.active);

// 2. Map to Names and Cities
const userLocations = users.map(u => ({ name: u.name, city: u.city }));

// 3. Group by City
const groupedByCity = users.reduce((acc, user) => {
    acc[user.city] = acc[user.city] || [];
    acc[user.city].push(user);
    return acc;
}, {});

// 4. Age Statistics
const ageStats = users.reduce((acc, user) => {
    acc.sum += user.age;
    acc.count++;
    return acc;
}, { sum: 0, count: 0 });

const averageAge = ageStats.sum / ageStats.count;

// 5. Get Unique Cities
const cities = [...new Set(users.map(u => u.city))];

// 6. Sort by Age
const sortedByAge = [...users].sort((a, b) => a.age - b.age);

// 7. Find Oldest User
const oldest = users.reduce((max, user) => 
    user.age > max.age ? user : max
);

🎯 Advanced Data Processing

// Data Pipeline
const pipeline = (data) => 
    data
        .filter(item => item.active)
        .map(item => ({
            ...item,
            ageGroup: item.age < 30 ? 'Young' : 'Senior'
        }))
        .sort((a, b) => a.age - b.age);

// Chunk Data
const chunk = (array, size) => 
    array.reduce((acc, _, i) => 
        i % size === 0 ? [...acc, array.slice(i, i + size)] : acc
    , []);

// Flatten Nested Arrays
const flatten = (arr) => arr.reduce((acc, val) => 
    acc.concat(Array.isArray(val) ? flatten(val) : val), []
);

// Deep Clone Objects
const deepClone = (obj) => 
    JSON.parse(JSON.stringify(obj));

// Merge Objects
const mergeObjects = (...objs) => 
    objs.reduce((acc, obj) => ({ ...acc, ...obj }), {});

// Performance Optimization
const processData = (data) => {
    // Use single-pass when possible
    let activeCount = 0;
    let totalAge = 0;
    
    for (const user of data) {
        if (user.active) {
            activeCount++;
            totalAge += user.age;
        }
    }
    
    return {
        activeCount,
        averageAge: totalAge / activeCount
    };
};

💡 Data Processing Tips

  • Use immutable operations
  • Consider performance for large datasets
  • Use meaningful variable names
  • Document complex transformations
  • Test edge cases

Array methods make data transformation elegant and readable. They’re essential for modern JavaScript development.

— Data Engineer

Related posts:

JavaScript: Use Async/Await for Clean Async Code

JavaScript: Use Async/Await for Clean Async Code

JavaScript: Preventing Memory Leaks with WeakMap and WeakSet

JavaScript: Preventing Memory Leaks with WeakMap and WeakSet

JavaScript: Use Nullish Coalescing (??) for Defaults

JavaScript: Use Nullish Coalescing (??) for Defaults

Views: 9

Post navigation

HTML: Semantic Elements for SEO and Accessibility
AJAX: REST API Security Best Practices

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com