1
days[
    {
      dt:2017-06-19T00:00:00.000Z,
      id:1,
      releases:[],
    },

    {
      dt:2017-06-20T00:00:00.000Z,
      id:2,
      releases:[{
               id:41,
               programId:2,
               teamId:116,
            }]
    },
    {
      dt:2017-06-21T00:00:00.000Z,
      id:3,
      releases:[]
    },
]

i want to delete the releases that have an id of 41 AND day.id of 2.. so my array should look like :

days[
    {
      dt:2017-06-19T00:00:00.000Z,
      id:1,
      releases:[],
    },

    {
      dt:2017-06-20T00:00:00.000Z,
      id:2,
      releases:[]
    },
    {
      dt:2017-06-21T00:00:00.000Z,
      id:3,
      releases:[]
    },
]

i have tried to filter :

var found = days.filter(function(day){
           return day.releases.filter(function(r){
                          return r.id===41
            });
 });

and then get the index and remove from there but i know there's a simpler way to do this.. can anyone help? There can be more than one releases in the nested releases object array so i need to specifically remove by looking at the release id and also the days id .

2 Answers 2

1

You could iterate days and filter only the releases which have the wanted id.

var days = [{ dt: '2017-06-19T00:00:00.000Z', id: 1, releases: [], }, { dt: '2017-06-20T00:00:00.000Z', id: 2, releases: [{ id: 41, programId: 2, teamId: 116, }] }, { dt: '2017-06-21T00:00:00.000Z', id: 3, releases: [] }];

days.forEach(function (day) {
    if (day.id === 2) {
        day.releases = day.releases.filter(function (release) {
            return release.id !== 41;
        });
    }
});

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

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

1 Comment

Excatly what i needed. Thanks!
1

You would like to use .map javascript prototype function to achieve this as follows-

var days = [
    {
      dt:'2017-06-19T00:00:00.000Z',
      id:1,
      releases:[],
    },

    {
      dt:'2017-06-20T00:00:00.000Z',
      id:2,
      releases:[{
               id:41,
               programId:2,
               teamId:116
            }]
    },
    {
      dt:'2017-06-21T00:00:00.000Z',
      id:3,
      releases:[]
    }
];

days = days.map(function(object){
      if (object.id === 2){
            object.releases = object.releases.filter(function (item) {
                return item.id !== 41
            });
      }
      return object;
});

console.log(days);

1 Comment

thanks! i want the current array to be changed. however the code works !

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.