Untitled
unknown
plain_text
a year ago
1.2 kB
7
Indexable
function processData(input, limit) {
const { type, data } = input;
const { labels, data: values } = data;
if (labels.length <= limit) {
return input; // If the number of labels is within the limit, return the input as is
}
// Take the first `limit - 1` labels and values
const limitedLabels = labels.slice(0, limit - 1);
const limitedValues = values.slice(0, limit - 1);
// Calculate the average for the remaining values
const remainingValues = values.slice(limit - 1);
const average = remainingValues.reduce((sum, val) => sum + val, 0) / remainingValues.length;
// Add "Other" to labels and the calculated average to values
limitedLabels.push("Other");
limitedValues.push(average);
return {
type,
data: {
labels: limitedLabels,
data: limitedValues,
},
};
}
// Example usage:
const input = {
type: "bar",
data: {
labels: ["Total Initiated", "Attempts", "Status Revert", "Success", "label 5", "label 6", "label 7", "label 8"],
data: [10, 20, 30, 40, 50, 60, 70, 80],
},
};
const limit = 4;
console.log(processData(input, limit));Editor is loading...
Leave a Comment