I'm trying to modify a string with Typescript. The string is created by the JSON.stringify() method.
I want to remove the properties "id", "lightStatus" and the "value" attributes of "inputPort" and "outputPort". (I only need their attribute "id".)
console.log(JSON.stringify(this.light));
// Results in -> {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}
I tried to do it the following way but it doesn't recognize "inputPort.id" and "outputPort.id". This is what I tried and what it resulted in.
var savedLight = JSON.stringify(this.light, ["name", "inputPort.id", "outputPort.id", "resistance"]);
// Results in -> {"name":"Light Switch","resistance":100}
The result should include the properties "name", "inputPort id", "outputPort id" and "resistance". Like this:
{"name":"Light Switch","inputPort": 2, "outputPort": 2, "resistance":100}
Can anyone help me how I can get rid of the unnecessary properties?
I'm trying to modify a string with Typescript. The string is created by the JSON.stringify() method.
I want to remove the properties "id", "lightStatus" and the "value" attributes of "inputPort" and "outputPort". (I only need their attribute "id".)
console.log(JSON.stringify(this.light));
// Results in -> {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}
I tried to do it the following way but it doesn't recognize "inputPort.id" and "outputPort.id". This is what I tried and what it resulted in.
var savedLight = JSON.stringify(this.light, ["name", "inputPort.id", "outputPort.id", "resistance"]);
// Results in -> {"name":"Light Switch","resistance":100}
The result should include the properties "name", "inputPort id", "outputPort id" and "resistance". Like this:
{"name":"Light Switch","inputPort": 2, "outputPort": 2, "resistance":100}
Can anyone help me how I can get rid of the unnecessary properties?
Share Improve this question edited Apr 1, 2017 at 14:38 eko 40.7k11 gold badges78 silver badges101 bronze badges asked Apr 1, 2017 at 14:29 BRsmoverBRsmover 8872 gold badges11 silver badges21 bronze badges 2- what is the expected json data and what you get from the response. update it clearly. – Aravind Commented Apr 1, 2017 at 14:32
- @Aravind I added the expected output. The result I got so far doesn't include the "inputPort" and the "outputPort" which is what I would want. – BRsmover Commented Apr 1, 2017 at 14:37
3 Answers
Reset to default 5You can pass a "replacer" function that returns the exact value you want.
var data = {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100};
var result = JSON.stringify(data, function(k, v) {
switch (k) {
case "": case "name": case "resistance":
return v
case "inputPort": case "outputPort":
return v.id
default:
return undefined;
}
}, 2)
document.querySelector("pre").textContent = result
<pre></pre>
The ""
represents the top level object. For that, "name"
, and "resistance"
, it simply returns the original value.
For "inputPort"
and "outputPort"
it returns the id
property.
Anything else gets undefined
, which means it gets omitted from the result.
You can use a replacer function for this.
var obj = {
"id": 1,
"name": "Light Switch",
"lightStatus": true,
"inputPort": {
"id": 2,
"value": 0
},
"outputPort": {
"id": 2,
"value": false
},
"resistance": 100
};
var stringified = JSON.stringify(obj, function(key, val) {
if (key === 'id' || key === 'lightStatus') {
return void(0);
}
if (key === 'inputPort' || key === 'outputPort') {
return val.id;
}
return val;
});
console.log(stringified);
You can apply Replacer
function of JSON.stringify
var data='{"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}';
var json=JSON.parse(data);
function replacer(i, val) {
switch (i) {
case "": case "name": case "resistance":
return val
case "inputPort": case "outputPort":
return val.id
default:
return undefined;
}
}
console.log(JSON.stringify(json,replacer));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389374a4434705.html
评论列表(0条)