1

var data = [
    {
        _id:            '6d2847090a6a1b',
        user_id:        '580da5653bd3cc5802f6c37c',
        name:           'aaaa',
        sum: 250
        
    },
    
    {
        _id:            '75f36d2847090a6a36580871',
        user_id:        '3bd3cc5802f6c37c580da565',
        name:           'bbbb',
       sum :30
    },
    {
        _id:            '6a3087184367090a6a3e58fd284',
        user_id:        '2f63bc580c37c580da565d3c',
        name:           'ccc',
        sum :100
    }
];


var giftedObject = [];
data.forEach(function(userSummary){
    userSummary.numberOfGifted = Math.floor(userSummary.sum / 100);
    for(var i = 0; i < userSummary.numberOfGifted; i++){
        giftedObject.push({
            user_id: userSummary.user_id,
            name: userSummary.name,
            gifted_point: 1,
            active: true
        });
    }
    
});
var result = {giftedObject: giftedObject};
console.log(result);

Above is my code .here when ever user reached 100 points giftedObjectobject will create.

if user reached 100 point that time one object will create

if user reached 200 point that time two object will create

after creating object we need to reduce the gifted values from data

expected result :

gifted object result:

{
        active:true
        user_id:        '580da5653bd3cc5802f6c37c',
        gifted_point: 1
        name:           'aaaa',


    },

    {
         active:true
        user_id:        '580da5653bd3cc5802f6c37c',
        gifted_point: 1
        name:           'aaaa',
    },
    {

         active:true
        user_id:        '2f63bc580c37c580da565d3c',
        gifted_point: 1
        name: 'ccc',
    }

final data result

 {
            _id:            '6d2847090a6a1b',
            user_id:        '580da5653bd3cc5802f6c37c',
            name:           'aaaa',
            sum: 248

        },

        {
            _id:            '75f36d2847090a6a36580871',
            user_id:        '3bd3cc5802f6c37c580da565',
            name:           'bbbb',
           sum :30
        },
        {
            _id:            '6a3087184367090a6a3e58fd284',
            user_id:        '2f63bc580c37c580da565d3c',
            name:           'ccc',
            sum :99
        }
2
  • 1
    It is not at all clear what you're trying to do. everything working fine but after creating object we need to reduce (minus) in sum value Commented Dec 14, 2016 at 17:43
  • Along the lines of @Jamiec, you want to create a new list of objects, while modifying the original list of objects in place to reduce the sum of each entry by the number of objects created for the new list? Commented Dec 14, 2016 at 17:45

2 Answers 2

2

You could subtract gifted_points from the sum.

var data = [{ _id: '6d2847090a6a1b', user_id: '580da5653bd3cc5802f6c37c', name: 'aaaa', sum: 250 }, { _id: '75f36d2847090a6a36580871', user_id: '3bd3cc5802f6c37c580da565', name: 'bbbb', sum: 30 }, { _id: '6a3087184367090a6a3e58fd284', user_id: '2f63bc580c37c580da565d3c', name: 'ccc', sum: 100 }],
    giftedObject = [];

data.forEach(function (userSummary) {
    var i,
        gifted_point = Math.floor(userSummary.sum / 100);

    userSummary.sum -= gifted_point;
    for (i = 0; i < gifted_point; i++) {
        giftedObject.push({
            user_id: userSummary.user_id,
            name: userSummary.name,
            gifted_point: 1,
            active: true
        });
    }
});

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

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

1 Comment

i need to get two different objects one for final data and one for giftedObject
0

How about decrementing the sum inside the for-loop?

for(var i = 0; i < userSummary.numberOfGifted; i++){
    giftedObject.push({
        user_id: userSummary.user_id,
        name: userSummary.name,
        gifted_point: 1,
        active: true
    });
    userSummary.sum -= 1; // <--- add this line
}

Comments

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.