0

i have one object and its having service array i checked if any service id and offer id match with object its decrease count value and once count reach zero need to remove that object.please check my try in fiddle. i can able to reduce but while reaching zero not able to remove

const obj = {
name:'saloon',
services:[
   {
     sid:1,
     offid:20,
     count:2
   },
   {
     sid:2,
     offid:18,
     count:1
   },
   {
     sid:3,
     offid:15,
     count:3
   }
]
}

given values : based on this service and offer id count should decrease once count reach zero need to remove that object

const servid = 2
const offid = 18

mycode

    obj.services = obj.services.map(data => {
      if(data.sid == servid && data.offid == offid ){
          return data.count > 1 && {
            sid:data.sid,
            offide:data.offid,
            count:data.count -1
          } 
        }else{
          return data
        }
    })
     console.log(obj);

expected result :

const result = {
            name:'saloon',
            services:[
                       {
                         sid:1,
                         offid:20,
                         count:2
                       },
                       {
                         sid:3,
                         offid:15,
                         count:3
                       }
                    ]
            }
3
  • Please include your code here, rather than linking to a fiddle. It means that people don't have to go to an external site to see the context. Commented Nov 29, 2017 at 23:00
  • @JustinBlank Hi okay now i included my code please check Commented Nov 29, 2017 at 23:05
  • 1
    @NarayananS Please consider using punctuation. The question is almost unreadable. Commented Nov 29, 2017 at 23:07

2 Answers 2

1

Use array#forEach to iterate through your array and check each sid and offid inside service, in case of match update the count value after that check if count value is less than or equal to zero, if it is, then using push its index into indexes array. After that, you can iterate through indexes array and delete those values in services array using array#splice.

const obj = { name:'saloon', services:[ { sid:1, offid:20, count:2 }, { sid:2, offid:18, count:1 }, { sid:3, offid:15, count:3 } ] };

const servid = 2;
const offid = 18;
var indexes = [];
obj.services.forEach(function(service, index) {
  if(service.sid === servid && service.offid === offid){
    --service.count;
  }
  if(service.count <= 0)
      indexes.push(index);
});

indexes.reverse().forEach(function(index){
  obj.services.splice(index, 1);
});
console.log(obj);

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

4 Comments

Slicing an array while going over it with forEach is a bad idea. stackoverflow.com/a/21811782/5734311
No problem, however your answer is now a duplicate of the answer I gave 5 hours earlier.
I have updated the solution using different approach.
This, again, will not work. Just like before, slicing obj.services will shift the indexes, and if there's more than one element in indexes, the second slice will target the wrong element.
1

Use .forEach() to decrease the count, then .filter() to remove elements with count == 0:

const obj = {
  name: 'saloon',
  services: [{
    sid: 1,
    offid: 20,
    count: 2
  }, {
    sid: 2,
    offid: 18,
    count: 1
  }, {
    sid: 3,
    offid: 15,
    count: 3
  }]
}

const servid = 2
const offid = 18

obj.services.forEach((data, i) => {
  if (data.sid == servid && data.offid == offid) {
    data.count--;
  }
})
obj.services = obj.services.filter(data => data.count > 0);

console.log(obj);

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.