Home > Enterprise >  Javascript - building object with number of occurrence from another object
Javascript - building object with number of occurrence from another object

Time:11-11

I have an object with the timestamp and I want to build another object with number of occurrences based on the minutes from the timestamp

Existing Object:

{
    "data": {
        "dataArr": [
            {
                "fields": {
                    "@timestamp": [
                         "2022-10-04T22:45:17.482Z"
                    ]
            }, 
            {
                "fields": {
                    "@timestamp": [
                         "2022-10-04T22:45:17.482Z"
                    ]
            },
            {
                "fields": {
                    "@timestamp": [
                         "2022-10-04T22:46:17.482Z"
                    ]
            }
        ]
    }
} 

My Code:

let arr = [];
const occData = data?. dataArr.map(function (val) {
    timestamp = val.fields["@timestamp"];
    var myDate = new Date(timestamp);
    var minutes = myDate.getMinutes(); // converted date to the minutes
    arr.push(minutes);
    // Now I need to find the occurrences and build the new object
});

Expected Result:

{
    "45": 2,
    "46": 1
}

CodePudding user response:

If you store the results as an object instead of an array you can get the intended results:

let arr = {};
d?.data?.dataArr.forEach(function (val) {
    timestamp = val.fields["@timestamp"];
    var myDate = new Date(timestamp);
    var minutes = myDate.getMinutes(); // converted date to the minutes
    arr[minutes] = 1   (arr[minutes]||0)
});

Results:

Object { 45: 2, 46: 1 }

CodePudding user response:

Assumptions

Your data was missing closing curly brackets for the fields objects, and I have assumed that because you're using map you want to assign output to the occData value.

I've also updated the usage of var and un-instantiated timestamp to constants as they are unchanging within their scope so do not need to be variable.

Solution

Following the convention of the method chaining style when using the map method on arrays, the typical way of converting the result to an object would be to reduce it.

The reduce function below is, for each minute value in the array resulting from the map function:

  • Taking either the existing value from the accumulator minuteObject or 0 if that value doesn't exist and adding 1 to it
  • Assigning the result back to the key for that minute value to the accumulator minuteObject and returning the accumulator for the next iteration

const existingObject = {
      "data": {
          "dataArr": [
              {
                  "fields": {
                      "@timestamp": [
                           "2022-10-04T22:45:17.482Z"
                      ]
                  }
              },
              {
                  "fields": {
                      "@timestamp": [
                           "2022-10-04T22:45:17.482Z"
                      ]
                  }
              },
              {
                  "fields": {
                      "@timestamp": [
                           "2022-10-04T22:46:17.482Z"
                      ]
                  }
              }
          ]
      }
  } 

const data = existingObject.data

const occData = data?.dataArr.map(val => {
      const timestamp = val.fields["@timestamp"];
      const myDate = new Date(timestamp);
      return myDate.getMinutes();
}).reduce((minuteObject, minute) => {
    minuteObject[minute] = (minuteObject[minute] || 0)   1;
    return minuteObject;
} , {});

console.log(occData);

  • Related