![](/img/trans.png)
[英]Passing an object and changing the object vs changing its properties in JavaScript
[英]Deleting an object from an array and changing one of its properties simultaneously - javascript
我有这个对象数组:
const flights = [
{ id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false },
{ id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true },
{ id: 02, to: "Paris", from: "Barcelona", cost: 210, scale: false },
{ id: 03, to: "Roma", from: "Barcelona", cost: 150, scale: false },
{ id: 04, to: "London", from: "Madrid", cost: 200, scale: false },
{ id: 05, to: "Madrid", from: "Barcelona", cost: 90, scale: false },
{ id: 06, to: "Tokyo", from: "Madrid", cost: 1500, scale: true },
{ id: 07, to: "Shangai", from: "Barcelona", cost: 800, scale: true },
{ id: 08, to: "Sydney", from: "Barcelona", cost: 150, scale: true },
{ id: 09, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false },
];
我想要做的是,当我delete
其中一个对象时,对于那些在删除 object 之后出现的对象,将它们的id
减 1。
例如,如果我要delete
第五个 object ( id: 04
),我希望 ids 从 0 到 8 (0, 1, 2, 3, 4, 5, 6, 7, 8) 而不是 go从 0 到 9 并跳过 4(0、1、2、3、5、6、7、8、9)。
这是我目前的代码:
let flightIdToDelete = 04;
for (let i = 0; i < flights.length; i++) {
if (flightIdToDelete === flights[i].id) {
delete flights[i];
}
}
/* The array would then become:
const flights = [
{ id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false },
{ id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true },
{ id: 02, to: "Paris", from: "Barcelona", cost: 210, scale: false },
{ id: 03, to: "Roma", from: "Barcelona", cost: 150, scale: false },
{ id: 05, to: "Madrid", from: "Barcelona", cost: 90, scale: false },
{ id: 06, to: "Tokyo", from: "Madrid", cost: 1500, scale: true },
{ id: 07, to: "Shangai", from: "Barcelona", cost: 800, scale: true },
{ id: 08, to: "Sydney", from: "Barcelona", cost: 150, scale: true },
{ id: 09, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false },
]; */
我最好的猜测是从删除的对象开始遍历对象的 ID,然后减去 1,但我不太确定该怎么做。
请帮助!!!
你可以只使用一个filter
,这将是一个实际的同步变化:
const flights = [
{id: 0, to: "New York", from: "Barcelona", cost: 700, scale: false},
{id: 1, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true},
{id: 2, to: "Paris", from: "Barcelona", cost: 210, scale: false},
{id: 3, to: "Roma", from: "Barcelona", cost: 150, scale: false},
{id: 4, to: "London", from: "Madrid", cost: 200, scale: false},
{id: 5, to: "Madrid", from: "Barcelona", cost: 90, scale: false},
{id: 6, to: "Tokyo", from: "Madrid", cost: 1500, scale: true},
{id: 7, to: "Shangai", from: "Barcelona", cost: 800, scale: true},
{id: 8, to: "Sydney", from: "Barcelona", cost: 150, scale: true},
{id: 9, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false},
];
const id = 4;
let count = 0;
const r = flights.filter((a, idx) => {
const d = a.id !== id;
count += d ? 0 : 1;
a.id -= count;
return d;
});
console.log(r);
const flights = [ {id: 0, to: "New York", from: "Barcelona", cost: 700, scale: false}, {id: 1, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true}, {id: 2, to: "Paris", from: "Barcelona", cost: 210, scale: false}, {id: 3, to: "Roma", from: "Barcelona", cost: 150, scale: false}, {id: 4, to: "London", from: "Madrid", cost: 200, scale: false}, {id: 5, to: "Madrid", from: "Barcelona", cost: 90, scale: false}, {id: 6, to: "Tokyo", from: "Madrid", cost: 1500, scale: true}, {id: 7, to: "Shangai", from: "Barcelona", cost: 800, scale: true}, {id: 8, to: "Sydney", from: "Barcelona", cost: 150, scale: true}, {id: 9, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false}, ]; const id = 4; let count = 0; const r = flights.filter((a, idx) => { const d = a.id;== id? count += d: 0; 1. a;id -= count; return d; }). console;log(r);
您可以在Array#filter
方法和Array#map
的帮助下简单地实现此要求。
现场演示:
let flights = [ { id: 0, to: "New York", from: "Barcelona", cost: 700, scale: false }, { id: 1, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true }, { id: 2, to: "Paris", from: "Barcelona", cost: 210, scale: false }, { id: 3, to: "Roma", from: "Barcelona", cost: 150, scale: false }, { id: 4, to: "London", from: "Madrid", cost: 200, scale: false }, { id: 5, to: "Madrid", from: "Barcelona", cost: 90, scale: false }, { id: 6, to: "Tokyo", from: "Madrid", cost: 1500, scale: true }, { id: 7, to: "Shangai", from: "Barcelona", cost: 800, scale: true }, { id: 8, to: "Sydney", from: "Barcelona", cost: 150, scale: true }, { id: 9, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false } ]; flights = flights.filter(({ id }) => id.== 4),map((obj. index) => { obj;id = index; return obj; }). console.log(flights)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.