miktam / key-del Goto Github PK
View Code? Open in Web Editor NEWDelete nested keys from Javascript object
License: MIT License
Delete nested keys from Javascript object
License: MIT License
Would be nice to have support for keys to remove inside of (nested) arrays as well since arrays are also valid parts of a JSON object.
Example:
let data = {
"myKey1": 123,
"myKey2": true,
"myKey3": {
"myKey4": "foo",
"myKey5": [
{
"myKey1": 456,
"myKey6": null
}
]
}
};
let keyDel = require("key-del");
keyDel(data, "myKey1");
Result should be...
{
"myKey2": true,
"myKey3": {
"myKey4": "foo",
"myKey5": [
{
"myKey6": null
}
]
}
}
...but is...
{
"myKey2": true,
"myKey3": {
"myKey4": "foo",
"myKey5": [
{
"myKey1": 456,
"myKey6": null
}
]
}
}
Hi,
I have a JSON object coming from Mongo:
{ "_id" : ObjectId("54a12e61c77c3ae01a84dba5"),
"user" : "demo",
"status" : "approved"
}
If I run:
return_obj = deleteKey(obj, fields);
where var fields = ["status"];
I get the following return_obj:
{ "_id": {
"_bsontype": "ObjectID",
"id": "U\ Ù�Þ�^��¶©�"
},
"user" : "demo"
}
I got a deep object and need to delete a prop 5 levels deep. This is apparently "not supported" yet. How come? It's just recursion?
It would be nice if we could delete keys from nested objects by their full path (in case the key is named the same in several different nested objects but you only want to delete some of them), i.e.:
var originalObject = {
one: 1,
two: 2,
three: {
nestedOne: 3,
nestedTwo: 4
},
four: {
nestedOne: 5,
nestedTwo: 6
}
}
var result = deleteKey(originalObject, ['one', 'three.nestedOne']) or like
var result = deleteKey(originalObject, ['one', '[three][nestedOne]')
While trying to delete a nested key, I am getting:
c:\node\project\node_modules\key-del\index.js:60
delete nestedObjectRef[lastAttribute];
^
TypeError: Cannot convert undefined or null to object
at c:\node\project\node_modules\key-del\index.js:60:34
at Array.forEach (native)
at deleteKeysFromObject (c:\node\project\node_modules\key-del\index.js:43:16)
at c:\node\project\node_modules\key-del\index.js:64:33
at Array.forEach (native)
at deleteKeysFromObject (c:\node\project\node_modules\key-del\index.js:43:16)
at c:\node\project\routes\v1\profile.js:229:58
at c:\node\project\node_modules\async\lib\async.js:254:17
at done (c:\node\project\node_modules\async\lib\async.js:135:19)
at c:\node\project\node_modules\async\lib\async.js:32:16
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.