简体   繁体   English

如何删除数组内的 object 的值

[英]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.编写一个名为 removePartyKillers 的 function,它接收一个类似“播放列表”(见下文)的数组,并返回该播放列表的副本,其中任何超过 8 分钟的歌曲都已被删除。 You can safely assume that a playlist is an array of objects.您可以放心地假设播放列表是一个对象数组。 Each object will have the properties title, artist, and durationInSeconds.每个 object 都将具有属性 title、artist 和 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,
  }
]
*/ 

I think .filter() is useful in this case.我认为.filter()在这种情况下很有用。

NOTE: awesomePlaylist itself is NOT an object (associative array) but an array;注意: awesomePlaylist本身不是 object(关联数组)而是一个数组; Therefore You need to manipulate an array.因此,您需要操作数组。 delete manipulates a property in an object. delete操作 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));

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.问题是密钥本身是一个 object,当它进入下一行时,您正在尝试访问 object 中每个可能属性的“durationofSeconds”。您可以使用密钥直接访问预期的属性并检查它。

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;
}

输出

delete is for removing object keys only. delete仅用于删除 object 密钥。 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 ):只需创建一个新的空数组并添加过滤后的项目(或者更好:使用.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));

As Miu has already said, delete statements are only used for object properties, not array elements.正如 Miu 已经说过的,删除语句仅用于 object 属性,而不是数组元素。

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.你需要向后 go 的原因是,如果你删除一个项目,下一个项目会收到它的索引,你最终会跳过它。

 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, } ] */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM