0

I am trying to change a specific key's value in json as below :

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"}]

i have tried looping in the input like input.forEach(element =>{//code})but somewhere iam missing.

3

2 Answers 2

0

This not works??

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) 
 })
Sign up to request clarification or add additional context in comments.

Comments

0

I have used spread operator: Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

example:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.