伙计们,美好的一天!我遇到了一些问题。我想从数组中删除所有数据,但不删除基于值的 1 个数据。例子:key = '2' array = [ 'data1': 'foo', 'data' : { 'id' : '1', 'id' : '2', 'id' : '3' } ]result = [ 'data1': 'foo', 'data' : { 'id' : '2' } ]就是这样,谢谢
2 回答

交互式爱情
TA贡献1712条经验 获得超3个赞
您可以使用filter()该值并将其与key.
const key = 'foo';
const array = ['foo', 'box', 'tree', 'bar'];
const result = array.filter(x => x === key);
console.log(result)

繁星点点滴滴
TA贡献1803条经验 获得超3个赞
最简单的方法是重新分配数组或使用结果创建一个新数组
const key = 'foo';
let array = ['foo', 'box', 'tree', 'bar'];
/*array = new Array(key); //be carefull if yoy are using this and the key is singular numeric value*/
array = [key];
console.log(array);
添加回答
举报
0/150
提交
取消