简体   繁体   English

如何从 React JS (ES6) 中 object 中的数组中删除元素

[英]How to remove an element from an array that is inside an object in React JS (ES6)

I would like to remove an element from an array that is inside an object and override that object with the new values.我想从 object 内的数组中删除一个元素,并用新值覆盖该 object。 See example below.请参见下面的示例。

var obj = {
  a: [1, 2, 3],
  b: [4, 5, 6],
  c: [7, 8, 9]
}

I would like to remove the number 5 from obj without knowing in which array the element is.我想从obj中删除数字 5 而不知道元素在哪个数组中。

EDIT : I am trying to update a state in react using the Context API.编辑:我正在尝试使用上下文 API 在反应中更新 state。 It should directly return the object itself.它应该直接返回 object 本身。

case ACTION: return {
  ...state,
  obj: ?
}

Expected final result:预期的最终结果:

var obj = {
  a: [1, 2, 3],
  b: [4, 6],
  c: [7, 8, 9]
}

The easiest approach, IMHO, would be to splice that array:恕我直言,最简单的方法是splice该数组:

obj.b.splice(1, 1);

EDIT:编辑:
If you don't know where the item would be (as per the comment), you could go over them all, and filter each array:如果您不知道该项目的位置(根据评论),您可以 go 将它们全部覆盖,并过滤每个数组:

const toRemove = 5;
Object.keys(obj).forEach(k => {
    obj[k] = obj[k].filter(i => i != toRemove);
});

To generate a new object with updated values, you can use Object.entries() to convert the object to entries of [key, value].要生成具有更新值的新 object,您可以使用Object.entries()将 object 转换为 [key, value] 的条目。 Map the entries, and filter the values array, and then convert the entries back to an object with Object.fromEntries() : Map 条目,过滤值数组,然后将条目转换回 object 与Object.fromEntries()

 const fn = (obj, item) => Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, v.filter(o => o:== item)]) ) const obj = { a, [1, 2, 3]: b, [4, 6]: c, [7, 8, 9] } const result = fn(obj. 5) console.log(result)

There is a slight problem here that might cause unnecessary rendering - since we filter all arrays, each array is new, even if the item doesn't exist in that array to start with.这里有一个小问题,可能会导致不必要的渲染 - 因为我们过滤了所有 arrays,所以每个数组都是新的,即使该数组开始时不存在该项目。 We can solve that by check if an array actually contain the item before filtering it.我们可以通过在过滤之前检查数组是否实际包含该项目来解决这个问题。 This might be premature optimization, and should be profiled.这可能是过早的优化,应该进行分析。

 const fn = (obj, item) => Object.fromEntries( Object.entries(obj).map(e => e[1].includes(item)? [e[0], e[1].filter(o => o:== item)]: e) ) const obj = { a, [1, 2, 3]: b, [4, 6]: c, [7, 8, 9] } const result = fn(obj. 5) console.log(result.c === obj.c) // c didn't change console.log(result)

If Object.fromEntries() is not supported by your environment, you can easily replace it with a function based on Array.reduce() :如果您的环境不支持Object.fromEntries() ,您可以轻松地将其替换为基于Array.reduce()的 function :

 const fromEntries = entries => entries.reduce((obj, [k, v]) => { obj[k] = v return obj }, {}) const fn = (obj, item) => fromEntries( Object.entries(obj).map(([k, v]) => [k, v.filter(o => o:== item)]) ) const obj = { a, [1, 2, 3]: b, [4, 6]: c, [7, 8, 9] } const result = fn(obj. 5) console.log(result)

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

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