df %>% filter(Alive == "y", Campaign == "FEB_gems",
Level >= 7, Game_n == "GAME1")
# Campaign Game_n Class Race Alignment Level Alive
# 1 FEB_gems GAME1 fighter elf CG 7 y
# 2 FEB_gems GAME1 sorcerer human NN 7 y
# 3 FEB_gems GAME1 rouge gnome LG 7 y
需要将{
"ID": "5c9b2de495e8d81ef437539e",
"WeightingFactor": "BIC",
"TargetAmount": 1000,
"FilterCondition": {
"_cls": "ComplexFilter",
"Condition": "AND",
"Rules": [{
"_cls": "Filter",
"Field": "MOODYSFOREIGNCURRENCYCEILINGRATING",
"Operator": "in list",
"Value": "A1,A2,A3"
},
{
"_cls": "Filter",
"Field": "ASOFDATE",
"Operator": "less or equal",
"Value": "26 Nov 2028"
},
{
"_cls": "ComplexFilter",
"Condition": "OR",
"Rules": [{
"_cls": "Filter",
"Field": "ASOFDATE",
"Operator": "less or equal",
"Value": "26 Nov 2028"
},
{
"_cls": "Filter",
"Field": "ASOFDATE",
"Operator": "less or equal",
"Value": "26 Nov 2029"
}
]
}
],
"Not": false
}
}
重命名为"field"
。请提出如何使用任何可用方法?
尝试了category
和stringify
。但这不起作用
预期输出:
replace
答案 0 :(得分:4)
您可以在给定的JSON字符串中将字符串"field":
替换为"category":
。
var json = JSON.stringify({ field: { field: 'foo' } }),
newJSON = json.replace(/"field":/g, '"category":');
console.log(JSON.parse(newJSON));
经典替代品
function replace(object, source, target) {
return Object.assign(...Object.entries(object).map(([k, v]) => ({
[k === source ? target : k]: v && typeof v === 'object'
? replace(v, source, target)
: v
})));
}
var object = { field: { field: 'foo' } },
newObject = replace(object, 'field', 'category');
console.log(newObject);
答案 1 :(得分:1)
我对Regex进行了性能测试,其中包括替换键和深度遍历JSON对象并替换键,结果发现Regex方法比深度遍历方法慢了65%。
下面是执行深度遍历的代码:
function deepTraverseAndReplace(o,sk,rk) {
for (var key in o) {
if (o[key ] !== null && typeof(o[key])=="object") {
traverse(o[key],sk,rk);
} else if( key == sk ) {
o[rk] = o[key];
delete(o[key]);
}
}
}
deepTraverseAndReplace(jsonObj,'Field','Category')
这是创建jsPerf的链接