如何解决如何使用索引而不是属性名称删除对象中的对象
原始代码
intent_uri
日志
let base = {
brand: 'Ford',model: '556',licensePlate: 55554,};
let carsObj = {};
let carsArr = [];
for (let i = 1; i < 6; i++) {
carsObj[`car${i}`] = {
brand: base.brand,model: base.model,licensePlate: base.licensePlate + i
}
carsArr.push({
brand: base.brand,licensePlate: base.licensePlate + i
});;
};
现在如何从carsObj 中删除car2 和car4,使用它的索引位置(如[0] 和[3]),而不是{{1 }} 然后将其存储在 newCarsObj 中,以便记录以下内容:
{
car1: { brand: 'Ford',licensePlate: 55555 },car2: { brand: 'Ford',licensePlate: 55556 },car3: { brand: 'Ford',licensePlate: 55557 },car4: { brand: 'Ford',licensePlate: 55558 },car5: { brand: 'Ford',licensePlate: 55559 }
}
not this answer in link - 因为这只是一个具有属性+值的对象。而不是对象中的对象 在 JavaScript 中寻找解决方案(没有 jQuery 、lodash 等) - 提前致谢。
解决方法
这正是您所需要的。
let obj = {
car1: { brand: 'Ford',model: '556',licensePlate: 55555 },car2: { brand: 'Ford',licensePlate: 55556 },car3: { brand: 'Ford',licensePlate: 55557 },car4: { brand: 'Ford',licensePlate: 55558 },car5: { brand: 'Ford',licensePlate: 55559 }
}
let removeItems = (arr) => {
arr.map(x => {
return Object.keys(obj)[x]
}).forEach(x => delete obj[x])
}
removeItems([1,2])
console.log(obj)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。