1

I am facing a little issue while inserting object data on specific index in Array.

Here is my JSON

let arr = [ 
        {"days":3, "count" : 10 },
    {"days":4, "count" : 12 },
    {"days":7, "count" : 14 },
    {"days":9, "count" : 20 }

]

Output, I would like to have.

updatedArr = [ 
             {"days":0, "count" : 0 },
             {"days":0, "count" : 0 },
             {"days":3, "count" : 10 },
             {"days":4, "count" : 12 },
             {"days":0, "count" : 0 },
             {"days":6, "count" : 34 },
]

so basically I want the object data to be pushed at specific index based on its key called days.

If the value of days is greater than 5 then it would be a sum of all the days value into one and push that at the last index e.g. 5th index.

Here is the code I have tried.

let arr = [{
    "days": 3,
    "count": 10
},
{
    "days": 4,
    "count": 12
},
{
    "days": 7,
    "count": 14
},
{
    "days": 9,
    "count": 20
}

]

for (let index = 0; index < 6; index++) {
const element = arr[index];


let eachObject = {}

eachObject.count = 0


if (element && element !== undefined && element.days && element.days <= 5) {

    eachObject.days = element.days
    eachObject.count = element.count

    arr.splice((element.days - 1), 0, eachObject)

} else if (element && element !== undefined && element.days > 5) {

    eachObject.days = element.days
    eachObject.count += Number.parseInt(element.count)

    arr.splice(4, 0, eachObject)
} else {
    eachObject.days = 0
    eachObject.count = 0
    arr.push(eachObject)
}

}

Any help would be great.

Thank You.

1 Answer 1

1

You could build a new data set with empty counts and update them with the actual values from the array.

Let start with the accumulator.

This is a new array, using Array.from with a length of 6 items and a mapping of an object with days and count properties.

Array#reduce takes an array and a startValue and returns the changed accumulator for each element.

Inside of the callback, an adjustment for the days property is made for greater value.

An update takes place for some properties of the element. Finally the accumulator r is returned.

let array = [{ days: 3, count: 10 }, { days: 4, count: 12 }, { days: 7, count: 14 }, { days: 9, count: 20 }],
    maxDays = 6,
    result = array.reduce(
        (r, { days, count }) => {
            days = Math.min(days, maxDays);
            r[days - 1].days = days;
            r[days - 1].count += count;
            return r;
        },
        Array.from({ length: maxDays }, () => ({ days: 0, count: 0 }))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

can you please explain this solution step by step.
thank you for your answer. Days would be started from 1 upto 2,3,4,5,5+ so I guess there will be 1 index shifting in array.
thank you for your answer. sometimes I get errors as days undefined even though I get days key in data.
@NinaScholz. it's because of Array.from({ length: 6 } . I might get days greater than 6 even sometimes 50 days. so How can I solve this ? Thank You.
please add some date with more than 6 days and the wanted result from it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.