Home > OS >  How to take out or change Key name of array-like object?
How to take out or change Key name of array-like object?

Time:10-15

I need to calculate the average scores per sub-array in javascript .. To do that i'm aiming to do a double loop for array[i][j].. but the problem is the key name or index per each sub-array is above 18k so my loop goes full overflow!!

by "my loop goes full overflow". I mean:

 for (let i = 0; i < array.length; i  ) {
  console.log(array[i]) 
 };

 /*
   will log =>> nothing stack overflow, stop your code before you start  
   to smell smoke 
 */

To do that i need to the change the key name or to take it out, but i don't know how. Any hint?? or guideline ??

Thanks

small example bellow of my inputs (i have for 1 user 4000 inputs more or less):

Current data :

i don't know how to represent it!!

enter image description here

![enter image description here

I don't know how to represent the structure showed in the images... is something like these

 const results = 
 {
   "18456": [
            {id: 1767, data: "blabla", score: 50, date:'2022-08-02T12:25:00.000Z'},
            {id: 1766, data: "blabla", score: 70, date: '2022-08-02T12:24:00.000Z'},
            {id: 1765, data: "blabla", score: 68, date: '2022-08-02T12:23:00.000Z'},
            {id: 1764, data: "blabla", score: 13, date: '2022-08-02T12:22:00.000Z'}
          ],
   "19212": [
            {id: 1763, data: "blabla", score: 20, date: '2022-07-15T12:25:00.000Z'},
            {id: 1762, data: "blabla", score: 75, date: '2022-07-15T12:25:00.000Z'},
            {id: 1761, data: "blabla", score: 80, date: '2022-07-15T12:25:00.000Z'},
            {id: 1760, data: "blabla", score: 53, date: '2022-07-15T12:25:00.000Z'}
          ],
   "19219": [
            {id: 1763, data: "blabla", score: 100, date: '2022-07-07T12:25:00.000Z'},
            {id: 1762, data: "blabla", score: 69, date: '2022-07-07T12:25:00.000Z'},
            {id: 1761, data: "blabla", score: 32, date: '2022-07-07T12:25:00.000Z'},
            {id: 1760, data: "blabla", score: 58, date: '2022-07-07T12:25:00.000Z'}
          ]
 }

Expected output :

 result = [
            {averageScore: 50.24%, date:'2022-08-02T12:25:00.000Z'},
            {averageScore: 57%, date: '2022-07-15T12:25:00.000Z'},
            {averageScore: 64.75%, date: '2022-07-07T12:25:00.000Z'},
          ]

Thanks much appreciated !!

CodePudding user response:

If your current data is like below

const arrayLikeObj =
{
  '18456': [
    { id: 1767, data: "blabla", score: 50, date: '2022-08-02T12:25:00.000Z' },
    { id: 1766, data: "blabla", score: 70, date: '2022-08-02T12:24:00.000Z' },
    { id: 1765, data: "blabla", score: 68, date: '2022-08-02T12:23:00.000Z' },
    { id: 1764, data: "blabla", score: 13, date: '2022-08-02T12:22:00.000Z' }
  ],
  '19212': [
    { id: 1763, data: "blabla", score: 20, date: '2022-07-15T12:25:00.000Z' },
    { id: 1762, data: "blabla", score: 75, date: '2022-07-15T12:25:00.000Z' },
    { id: 1761, data: "blabla", score: 80, date: '2022-07-15T12:25:00.000Z' },
    { id: 1760, data: "blabla", score: 53, date: '2022-07-15T12:25:00.000Z' }
  ],
  '19219': [
    { id: 1763, data: "blabla", score: 100, date: '2022-07-07T12:25:00.000Z' },
    { id: 1762, data: "blabla", score: 69, date: '2022-07-07T12:25:00.000Z' },
    { id: 1761, data: "blabla", score: 32, date: '2022-07-07T12:25:00.000Z' },
    { id: 1760, data: "blabla", score: 58, date: '2022-07-07T12:25:00.000Z' }
  ]
};

You can use this for getting the desired result.

// Extract only values
const result = Object.values(arrayLikeObj).map(m => {
  return {
    // Iterate over every nested object and get the mean of included scores.
    averageScore: m.reduce((acc, obj) => {
      return acc   obj.score;
    }, 0) / m.length   '%',
    // Get first date of nested object.
    date: m.find(f => f).date
  }
});

Test Section:

const arrayLikeObj = {
  '18456': [{
      id: 1767,
      data: "blabla",
      score: 50,
      date: '2022-08-02T12:25:00.000Z'
    },
    {
      id: 1766,
      data: "blabla",
      score: 70,
      date: '2022-08-02T12:24:00.000Z'
    },
    {
      id: 1765,
      data: "blabla",
      score: 68,
      date: '2022-08-02T12:23:00.000Z'
    },
    {
      id: 1764,
      data: "blabla",
      score: 13,
      date: '2022-08-02T12:22:00.000Z'
    }
  ],
  '19212': [{
      id: 1763,
      data: "blabla",
      score: 20,
      date: '2022-07-15T12:25:00.000Z'
    },
    {
      id: 1762,
      data: "blabla",
      score: 75,
      date: '2022-07-15T12:25:00.000Z'
    },
    {
      id: 1761,
      data: "blabla",
      score: 80,
      date: '2022-07-15T12:25:00.000Z'
    },
    {
      id: 1760,
      data: "blabla",
      score: 53,
      date: '2022-07-15T12:25:00.000Z'
    }
  ],
  '19219': [{
      id: 1763,
      data: "blabla",
      score: 100,
      date: '2022-07-07T12:25:00.000Z'
    },
    {
      id: 1762,
      data: "blabla",
      score: 69,
      date: '2022-07-07T12:25:00.000Z'
    },
    {
      id: 1761,
      data: "blabla",
      score: 32,
      date: '2022-07-07T12:25:00.000Z'
    },
    {
      id: 1760,
      data: "blabla",
      score: 58,
      date: '2022-07-07T12:25:00.000Z'
    }
  ]
};

// Extract only values
const result = Object.values(arrayLikeObj).map(m => {
  return {
    // Iterate over every nested object and get mean of them.
    averageScore: m.reduce((acc, obj) => {
      return acc   obj.score;
    }, 0) / m.length   '%',
    // Get first date of nested object.
    date: m.find(f => f).date
  }
});

console.log(result)

  • Related