我是 Javascript 的新手。我有以下 Json 資料。 輸入
let data = {
"filters": {
"operator": "AND",
"args": [
{
"operator": "OR",
"args": [
{
"operator": "EQ",
"name": "consumer_id",
"args": [
"200"
],
"type": "string"
},
{
"operator": "AND",
"args": [
{
"operator": "RANGE",
"name": "created_date",
"args": [
{"from":1},{"to":2}
],
"type": "number"
},
{
"operator": "EQ",
"name": "state",
"args": [
"TN"
],
"type": "string"
}
]
}
]
},
{
"operator": "GE",
"name": "Character",
"args": [
"H"
],
"type": "string"
}
]
}
我正在嘗試使用以下更改創建一個新的 Json。
- 將鍵名從名稱替換為列。
- 找到運算子“RANGE”后,使用 LE 和 GE 將其拆分為 2 個運算子。
預期產出
{
"filters": {
"operator": "AND",
"args": [
{
"operator": "OR",
"args": [
{
"operator": "EQ",
"column": "consumer_id",
"args": [
"200"
],
"type": "string"
},
{
"operator": "AND",
"args": [
{
"operator": "LE",
"column": "created_date",
"args": [
1
],
"type": "number"
},
{
"operator": "GE",
"column": "created_date",
"args": [
2
],
"type": "number"
},
{
"operator": "EQ",
"column": "state",
"args": [
"TN"
],
"type": "string"
}
]
}
]
},
{
"operator": "GE",
"column": "character",
"args": [
"H"
],
"type": "string"
}
]
}
}
我的代碼:
data.filters.args.filter( item => {
iterateObject(item);
});
function iterateObject(obj) {
for(var prop in obj) {
if(typeof(obj[prop]) === "object" ) {
iterateObject(obj[prop]);
} else {
if (prop === "operator" && obj[prop] === "RANGE") {
obj={
"LE": {"operator": "LE",
"column": obj.name,
"args": [obj.args[0].from],
"type": obj.type
},
"GE":{
"operator": "GE",
"column": obj.name,
"args": [obj.args[1].to],
"type": obj.type
}
}
console.log(obj) // The object gets replaced here, but I don't know how to map with original Json( ie, to the variable data )
}
if (prop === "name" ) {
prop="column"
}
}
}
}
console.log(JSON.stringify(data));
題:
我能夠成功實作更改 1 我嘗試了多種方法來實作更改 2,但我做不到。請幫忙
uj5u.com熱心網友回復:
這應該可以解決問題:
- 檢查物件的
name屬性;如果存在,將其復制到column屬性并洗掉obj.name - 檢查
obj.operator === 'RANGE'; 如果true,- 重命名
operator為AND - 創建兩個新的單操作物件并將其添加到
args - 洗掉
column和type欄位
- 重命名
例子
let data = {
"filters": {
"operator": "AND",
"args": [
{
"operator": "OR",
"args": [
{
"operator": "EQ",
"name": "consumer_id",
"args": [
"200"
],
"type": "string"
},
{
"operator": "AND",
"args": [
{
"operator": "RANGE",
"name": "created_date",
"args": [
{"from":1},{"to":2}
],
"type": "number"
},
{
"operator": "EQ",
"name": "state",
"args": [
"TN"
],
"type": "string"
}
]
}
]
},
{
"operator": "GE",
"name": "Character",
"args": [
"H"
],
"type": "string"
}
]
}
};
function modify(obj) {
if (!obj) return;
if (obj.name) {
obj.column = obj.name
delete obj.name;
}
if (obj.operator === 'RANGE') {
obj.operator = 'AND';
obj.args = [
{
"operator": "GE",
"column": obj.column,
"args": [ obj.args[0].from ],
"type": obj.type
},
{
"operator": "LE",
"column": obj.column,
"args": [ obj.args[1].to ],
"type": obj.type
}
];
delete obj.column;
delete obj.type;
}
for (const arg of obj.args || []) {
modify(arg);
}
}
modify(data.filters);
console.log(JSON.stringify(data, null, 2));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/324660.html
標籤:javascript 节点.js json 对象字面量
