从数组中删除一个对象,然后更新所有对象的 属性
Remove an object from the array then update the property of all objects
我想通过 id 从数组中删除一个对象,然后更新所有对象的位置 属性。例如我有一个像这样的数组:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 12, position: 3},
{id: 13, position: 4}
]
示例: 首先我需要删除 ID 为 12 的对象,然后更改每个对象的位置 属性。结果:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 13, position: 3}
]
我的尝试:
const fn = id => pipe(
reject(propEq('id', id)),
map((item, key) => evolve({position: key}))
);
通过将 R.addIndex
应用于 R.map
创建一个 mapIndexed
函数。这是必需的,因为 R.map
不会将当前索引传递给回调函数。
此外,您需要传递一个函数给 R.evolve
来设置 position
(通过 R.always
或箭头函数),您还需要通过item
进化:
const { pipe, reject, propEq, addIndex, map, evolve, always } = R;
const mapIndexed = addIndex(map);
const fn = id => pipe(
reject(propEq('id', id)),
mapIndexed((item, key) => evolve({ position: always(key + 1) }, item))
);
const arr = [{"id":10,"position":1},{"id":11,"position":2},{"id":12,"position":3},{"id":13,"position":4}];
const result = fn(11)(arr);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
在这种情况下我会跳过 R.evolve
,并在 item
上使用 spread 生成新对象,并分配新的 position
:
const { pipe, reject, propEq, addIndex, map } = R;
const mapIndexed = addIndex(map);
const fn = id => pipe(
reject(propEq('id', id)),
mapIndexed((item, key) => ({ ...item, position: key + 1 }))
);
const arr = [{"id":10,"position":1},{"id":11,"position":2},{"id":12,"position":3},{"id":13,"position":4}];
const result = fn(11)(arr);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
您可以使用 Array.filter()
删除和 Array.map()
重新定位项目,如下所示:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 12, position: 3},
{id: 13, position: 4}
]
repositionedArray = arr.filter(item => item.id != 11).map((item, index) => ({...item, position: index+1}))
console.log(repositionedArray);
希望对您有所帮助
我想通过 id 从数组中删除一个对象,然后更新所有对象的位置 属性。例如我有一个像这样的数组:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 12, position: 3},
{id: 13, position: 4}
]
示例: 首先我需要删除 ID 为 12 的对象,然后更改每个对象的位置 属性。结果:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 13, position: 3}
]
我的尝试:
const fn = id => pipe(
reject(propEq('id', id)),
map((item, key) => evolve({position: key}))
);
通过将 R.addIndex
应用于 R.map
创建一个 mapIndexed
函数。这是必需的,因为 R.map
不会将当前索引传递给回调函数。
此外,您需要传递一个函数给 R.evolve
来设置 position
(通过 R.always
或箭头函数),您还需要通过item
进化:
const { pipe, reject, propEq, addIndex, map, evolve, always } = R;
const mapIndexed = addIndex(map);
const fn = id => pipe(
reject(propEq('id', id)),
mapIndexed((item, key) => evolve({ position: always(key + 1) }, item))
);
const arr = [{"id":10,"position":1},{"id":11,"position":2},{"id":12,"position":3},{"id":13,"position":4}];
const result = fn(11)(arr);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
在这种情况下我会跳过 R.evolve
,并在 item
上使用 spread 生成新对象,并分配新的 position
:
const { pipe, reject, propEq, addIndex, map } = R;
const mapIndexed = addIndex(map);
const fn = id => pipe(
reject(propEq('id', id)),
mapIndexed((item, key) => ({ ...item, position: key + 1 }))
);
const arr = [{"id":10,"position":1},{"id":11,"position":2},{"id":12,"position":3},{"id":13,"position":4}];
const result = fn(11)(arr);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
您可以使用 Array.filter()
删除和 Array.map()
重新定位项目,如下所示:
const arr = [
{id: 10, position: 1},
{id: 11, position: 2},
{id: 12, position: 3},
{id: 13, position: 4}
]
repositionedArray = arr.filter(item => item.id != 11).map((item, index) => ({...item, position: index+1}))
console.log(repositionedArray);
希望对您有所帮助