我正在尝试更改json中特定键的值,如下所示:
input = [{"201708":10,"201709": 12, "metric":"attritionManaged"},{"201708":10,"201709": 12, "metric":"attritionUnManaged"},{"201708":10,"201709": 12, "metric":"EHC"}]
output= [{"201708":10,"201709": 12, "metric":"managedAttrition"},{"201708":10,"201709": 12, "metric":"unmanagedAttrition"},{"201708":10,"201709": 12, "metric":"EndingHeadcount"}]
我已尝试在input.forEach(element =>{//code})
之类的输入中循环,但在某处我失踪了。
答案 0 :(得分:0)
这不起作用??
let input = [{"201708":10,"201709": 12, "metric":"attritionManaged"},{"201708":10,"201709": 12, "metric":"attritionUnManaged"},{"201708":10,"201709": 12, "metric":"EHC"}]
input.forEach(i=>{
i.metric = 'abc'
})
input.forEach(i=>{
alert(i.metric)
})
答案 1 :(得分:0)
我使用过传播运算符: Spread语法允许在可能需要零个或多个参数(用于函数调用)或元素(用于数组文字)的位置扩展数组表达式或字符串等迭代,或者在零或更多位置扩展对象表达式键值对(对象文字)是预期的。
示例:
var a = [1,2,3]
var b = [4,5,6]
a.push(...b)
a //which is [1,2,3,4,5,6]
input = [{"201708":10,"201709": 12, "metric":"attritionManaged"},{"201708":10,"201709": 12, "metric":"attritionUnManaged"},{"201708":10,"201709": 12, "metric":"EHC"}]
var output = input;
output.forEach(function(currentValue, index, arr){
currentValue.metric = getString(currentValue.metric);
})
function getString(str){
var arr = [];
var arr1 = [];
var isUpperArrived = 0;
if(str.length>0)
{
arr.push(str[0].toUpperCase());
}
for(var i=1;i<str.length;i++)
{
if(str[i]==str[i].toUpperCase())
{
isUpperArrived=1;
arr1.push(str[i].toLowerCase());
}else{
if(!isUpperArrived)
{
arr.push(str[i]);
}else{
arr1.push(str[i]);
}
}
}
//pushing arr using spread operator
arr1.push(...arr);
return arr1.join('');
}
console.log(output);
&#13;