[英]How can i remove a field from an object
I have an array of object.我有一个 object 数组。 For doing one opetation I need to remove few fields from this object and for some other operation I have to use the whole fields
为了进行一项操作,我需要从此 object 中删除几个字段,而对于其他一些操作,我必须使用整个字段
But both of the array removing "regex" field.但是这两个数组都删除了“正则表达式”字段。 What is the mistake I am doing here?
我在这里做错了什么?
var newob = {}; var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; newob = JSON.stringify(myObject); delete newob.regex; console.log("Test1", newob); console.log("Test2", myObject);
You're missing JSON.parse
so you can create the object back.您缺少
JSON.parse
因此您可以创建 object 回来。 Otherwise, you're trying to delete
a property regex
of a string
, which doesn't exist.否则,您将尝试
delete
不存在的string
的属性regex
。
newob = JSON.parse(JSON.stringify(myObject));
JSON.stringify
creates a string, you need to do JSON.parse
to create an object from a JSON string. JSON.stringify
创建一个字符串,您需要执行JSON.parse
从 Z0ECD111C1D7A2D87A20 字符串创建一个 object。
For that object, you can use Object.assign({}, myObject)
since it's a shallow clone.对于 object,您可以使用
Object.assign({}, myObject)
因为它是一个浅克隆。
newob = Object.assign({}, myObject);
// newobj = { ...myObject } // this will also work
The problem is that newObj
is not a copy of myObject
but a reference to it.问题是
newObj
不是myObject
的副本,而是对它的引用。 Thus when you delete the filed of myObject
you also see the change in newObj
因此,当您删除
myObject
的字段时,您还会看到newObj
的变化
To explain what I have said, look at this snippet:为了解释我所说的,请看这个片段:
> const a = {a: 1}
undefined
> b = a
{ a: 1 }
> a.c = 58
58
> b
{ a: 1, c: 58 }
You can copy the object like this:您可以像这样复制 object:
const newobj = JSON.parse(JSON.stringify(myObject));
then changes on myObject
won't affect newobj
那么
myObject
上的更改不会影响newobj
There is an other problem:还有一个问题:
newob = JSON.stringify(myObject);
is wrong here because you assign you want an object but JSON.stringify returns a string在这里是错误的,因为你指定你想要一个 object 但 JSON.stringify 返回一个字符串
If I understand you correctly you want to map the input data and remove the regex
from each of the objects in your input array.如果我理解正确,您希望 map 输入数据并从输入数组中的每个对象中删除
regex
。
const items = [
{
ircEvent: 'PRIVMSG',
method: 'newURI',
regex: '^http://.*',
},
{
ircEvent: 'TEST',
method: 'newURI',
regex: '^http://.*',
},
]
const newItems = items.map(({ regex, ...item }) => item)
A good way to explain what is happening above is解释上面发生的事情的一个好方法是
const newArray = array.map(({dropAttr1, ...keepAttrs}) => keepAttrs)
But should you want to remove the key from one object only you could但是如果您想从一个 object 中删除密钥,只有您可以
const myObject = {
ircEvent: 'PRIVMSG',
method: 'newURI',
regex: '^http://.*',
}
const { regex, ...noRegex } = myObject
console.log(noRegex)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.