简体   繁体   中英

Modifying the values inside an object

I am trying to alter object returned from an api to have a similar structure to another data set in my app and push them into an Array. So that I can add this data to my original data. But I am getting a strange result from my reduce function.

The data is coming in like this:

 {
  count: 4,
  datapoints: [
    {
      Date: "2021-05-22",
      score: 0,
    },
    {
      Date: "2021-05-23",
      score: 0,
    },
    {
      Date: "2021-05-24",
      score: 0,
    },
    {
      Date: "2021-05-25",
      score: 114,
    },
  ],
};

I have a reduce function that looks like this:

const riskScores = await api.PCC.riskAssessment(userID, startdate, endDate);

const riskScoresFormatted = riskScores.datapoints.reduce((result, data) => {
        const scores = result["riskAssessment"] || [];
        scores.push({
          value: data.score,
          unit: "none",
          recordedDate: data.Date,
          method: "none",
        });
        result["riskAssessment"] = scores;
        return result;
      });

When I console out riskScoresFormatted Im getting something that looks like what I want but its coming out with the newly formatted objects nested inside the first object iterated over. like this:

{ Date: "2021-05-22"
  riskAssessment: [{value: 0, unit: "none", recorded...}{...}] <- the data I need
  score: 0
}

So I see clearly Im doing something wrong. I want to have all the newly formatted objects pushed inside an array with the key of "riskAssessment". Can anyone spot what Im doing wrong?

Provide the empty array [] as the initial data for the array at the end of reduce function.

const riskScoresFormatted = riskScores.datapoints.reduce((result, data) => {
        const scores = result["riskAssessment"] || [];
        scores.push({
          value: data.score,
          unit: "none",
          recordedDate: data.Date,
          method: "none",
        });
        result["riskAssessment"] = scores;
        return result;
      },[]);

Output

[
    {
        "value": 0,
        "unit": "none",
        "recordedDate": "2021-05-22",
        "method": "none"
    },
    {
        "value": 0,
        "unit": "none",
        "recordedDate": "2021-05-23",
        "method": "none"
    },
    {
        "value": 0,
        "unit": "none",
        "recordedDate": "2021-05-24",
        "method": "none"
    },
    {
        "value": 114,
        "unit": "none",
        "recordedDate": "2021-05-25",
        "method": "none"
    }
]

From what I gather, you want riskAssessment to be a new array with your new objects.

You're setting scores to be the previous value of riskAssessment . It will inherit the previous state, and you're just adding to it. Try to initialize it as an empty array directly, and then push your values to it.

const riskScoresFormatted = riskScores.datapoints.reduce((result, data) => {
  const scores = [];
  scores.push({
    value: data.score,
    unit: "none",
    recordedDate: data.Date,
    method: "none",
  });
  result["riskAssessment"] = scores;
  return result;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM