JavaScript Quickest way to make schema of objects in Array of Object uniform

I am having a big array (length:10k) of coordinates (rawData = [x,y1,y2,y3,...]) as follows:

rawData = [
    {x: 1, y1: 1, y2: 13, y3: 2},
    {x: 2, y1: 2, y2: 23, y3: 2},
    {x: 3, y1: 3, y2: 33, y3: 2, y4: 4},
    {x: 4, y1: 4, y2: 43, y3: 2, y5: 5},
]

As it can be seen, the objects in this array are not consistent because of varying schema.

What is the fastest way to make this array consistent, so that they have same schema?

If the object is not having any given key, it can be initialized with a default value (say 0) to make processedData as follows:

processedData = [
    {x: 0, y1: 1, y2: 13, y3: 2, y4: 0, y5: 0},
    {x: 1, y1: 2, y2: 23, y3: 2, y4: 0, y5: 0},
    {x: 2, y1: 3, y2: 33, y3: 2, y4: 4, y5: 0},
    {x: 3, y1: 4, y2: 43, y3: 2, y4: 0, y5: 5},
]

Edit: I am expecting a solution without needing to loop the entire array because this array is huge, or some solution optimized for that.

Answer:1

You can use Array.map and template object

const rawData = [
    {x: 1, y1: 1, y2: 13, y3: 2},
    {x: 2, y1: 2, y2: 23, y3: 2},
    {x: 3, y1: 3, y2: 33, y3: 2, y4: 4},
    {x: 4, y1: 4, y2: 43, y3: 2, y5: 5},
];

const temp = rawData
.reduce((t, o) => ({...t, ...o}), {});

Object.keys(temp).forEach(k => temp[k] = 0);

const result = rawData.map(o => ({...temp, ...o}));

console.log(result);
Answer:2

You could take a single loop for the data and update the keys if an unknown keys is found.

var rawData = [{ x: 1, y1: 1, y2: 13, y3: 2 }, { x: 2, y1: 2, y2: 23, y3: 2 }, { x: 3, y1: 3, y2: 33, y3: 2, y4: 4 }, { x: 4, y1: 4, y2: 43, y3: 2, y5: 5 }],
    keys = [],
    processedData = rawData.reduce((r, o) => {
        var temp = Object.assign({}, o);
        Object.keys(o).forEach(k => {
            if (keys.includes(k)) {
                return;
            }
            r.forEach(p => Object.assign(p, { [k]: 0 }));
            keys.push(k);
        });
        r.push(temp)
        return r;
    }, []);
    
console.log(processedData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3

I have an array like this: const teamsPoints = [ {team1_game00: 1}, {team1_game01: 2}, {team1_game02: 3}, {team2_game00: 0}, {team2_game10: 2}, {team2_game11: 3}, {team3_game01: 0}, {...

I have an array like this: const teamsPoints = [ {team1_game00: 1}, {team1_game01: 2}, {team1_game02: 3}, {team2_game00: 0}, {team2_game10: 2}, {team2_game11: 3}, {team3_game01: 0}, {...

  1. reduced order method

I have an array object where there are key value pairs. I am trying to get the keys in that array using a loop but I am getting only 0. What is the problem with my code. var strj = '{"name":"John",...

I have an array object where there are key value pairs. I am trying to get the keys in that array using a loop but I am getting only 0. What is the problem with my code. var strj = '{"name":"John",...

  1. cannot open .get/fetch_head
  2. apt-get cannot fetch
  3. fetch cannot get
  4. you-get cannot fetch video
  5. fetch cannot get headers

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

  1. flatmap works