Ryan Farm
Ryan Farm

Reputation: 31

How to delete a value of an object inside of an array

How come this isn't working? Everything seems right to me. Am I missing something?

Write a function called removePartyKillers that takes in an array like "playlist" (see below) and returns a copy of that playlist where any song longer than 8 minutes has been removed. You can safely assume that a playlist is an array of objects. Each object will have the properties title, artist, and durationInSeconds.

var awesomePlaylist = [
  {
    title: "Hay Day",
    artist: "Robo-Crop",
    durationInSeconds: 378
  }, {
    title: "10,000 Pounds",
    artist: "L-Ton Jonn",
    durationInSeconds: 498,
  }, {
    title: "Caesar's Salad",
    artist: "DJ Dinner Julius",
    durationInSeconds: 600,
  }, {
    title: "The British Are On Their Way",
    artist: "Raul Pevere",
    durationInSeconds: 1095,
  }, {
    title: "13th",
    artist: "The Doctors",
    durationInSeconds: 185,
  }
];

function removePartyKillers(arr) {
    var copy = arr.slice();
    for (var key of copy) {
        for (var key2 in key) {
            if (key2['durationInSeconds'] > (60 * 8)) {
                delete key2;
            }
        }
    }
    return copy;
}

console.log(removePartyKillers(awesomePlaylist));
 /* Results:
  {
    title: "Hay Day",
    artist: "Robo-Crop",
    durationInSeconds: 378
  }, {
    title: "13th",
    artist: "The Doctors",
    durationInSeconds: 185,
  }
]
*/ 

Upvotes: 3

Views: 131

Answers (4)

teddcp
teddcp

Reputation: 1624

The problem is key itself is an object and when it goes to the next line, you are trying to access the 'durationofSeconds' of every possible properties inside the object .You can directly access the intended property with key and check it.

function removePartyKillers(arr) {
    var copy = arr.slice();
    for (var key of copy) {
            if (key['durationInSeconds'] > (60 * 8)) {
                delete key['durationInSeconds'];
            
            //console.log(key)
        }
    }
    return copy;
}

output

Upvotes: 1

Benjamin Davies
Benjamin Davies

Reputation: 354

As Miu has already said, delete statements are only used for object properties, not array elements.

The one of the best ways to accomplish this, without using filter, is to loop backwards through the array, removing items if you need to. The reason why you need to go backward is because, if you remove an item, the next item receives its index and you end up skipping it.

var awesomePlaylist = [
  {
    title: "Hay Day",
    artist: "Robo-Crop",
    durationInSeconds: 378
  }, {
    title: "10,000 Pounds",
    artist: "L-Ton Jonn",
    durationInSeconds: 498,
  }, {
    title: "Caesar's Salad",
    artist: "DJ Dinner Julius",
    durationInSeconds: 600,
  }, {
    title: "The British Are On Their Way",
    artist: "Raul Pevere",
    durationInSeconds: 1095,
  }, {
    title: "13th",
    artist: "The Doctors",
    durationInSeconds: 185,
  }
];

function removePartyKillers(arr) {
    var copy = arr.slice();
    for (var i = copy.length - 1; i >= 0; i--) {
        var obj = copy[i];
        if (obj['durationInSeconds'] > (60 * 8)) {
            copy.splice(i, 1);
        }
    }
    return copy;
}

console.log(removePartyKillers(awesomePlaylist));
 /* Results:
  {
    title: "Hay Day",
    artist: "Robo-Crop",
    durationInSeconds: 378
  }, {
    title: "13th",
    artist: "The Doctors",
    durationInSeconds: 185,
  }
]
*/

Upvotes: 1

Đinh Carabus
Đinh Carabus

Reputation: 3496

delete is for removing object keys only. You cannot remove items of an array with it. Just create a new empty array and add the filtered items (or even better: use .filter):

var awesomePlaylist = [
  {
    title: "Hay Day",
    artist: "Robo-Crop",
    durationInSeconds: 378
  }, {
    title: "10,000 Pounds",
    artist: "L-Ton Jonn",
    durationInSeconds: 498,
  }, {
    title: "Caesar's Salad",
    artist: "DJ Dinner Julius",
    durationInSeconds: 600,
  }, {
    title: "The British Are On Their Way",
    artist: "Raul Pevere",
    durationInSeconds: 1095,
  }, {
    title: "13th",
    artist: "The Doctors",
    durationInSeconds: 185,
  }
];

function removePartyKillers(arr) {
    var copy = [];
    for (var item of arr) {
        if (item['durationInSeconds'] <= (60 * 8)) {
            copy.push(item);
        }
    }
    return copy;
}

console.log(removePartyKillers(awesomePlaylist));

Upvotes: 1

Miu
Miu

Reputation: 844

I think .filter() is useful in this case.

NOTE: awesomePlaylist itself is NOT an object (associative array) but an array; Therefore You need to manipulate an array. delete manipulates a property in an object.

let awesomePlaylist = [{
  title: "Hay Day",
  artist: "Robo-Crop",
  durationInSeconds: 378
}, {
  title: "10,000 Pounds",
  artist: "L-Ton Jonn",
  durationInSeconds: 498,
}, {
  title: "Caesar's Salad",
  artist: "DJ Dinner Julius",
  durationInSeconds: 600,
}, {
  title: "The British Are On Their Way",
  artist: "Raul Pevere",
  durationInSeconds: 1095,
}, {
  title: "13th",
  artist: "The Doctors",
  durationInSeconds: 185,
}];

function removePartyKillers(arr) {
  return arr.filter(song => song.durationInSeconds <= 8*60)
}

console.log(removePartyKillers(awesomePlaylist));

Upvotes: 5

Related Questions