Symmetric Difference stringify and replace

Hello i am doing Symmetric Difference and was thniking to use JSON.stringify on Arguments and then replace it with regEx but it didn’t work i am wondering why ?

var newArr= JSON.stringify(arguments); // "{'0':[1,2,3],'1':[5,2,1,4]}"
newArr= newArr.replace(/'[\d]\'/g,""); //it didn't delete the keys then i have another 
newArr= newArr.replace(/[{}:\[\]]/g, "");  //this one worked
//and then i wanted to use filter or somethink iwoul chainit  

I used another approach but i still wondering why it didn’t worked?

Best is to put the arguments into an array and then do a reduce on that array.

var testAry = [];
	testAry = Array.from(arguments).reduce(someCallbackFunc);

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

Sry but i’m not asking what is best but i asking why it doesn’t work with replace ? the keys with “”?
or why it is wrong in general what artefacts it can generat

Maybe because of missing :

it Works for Second Replace but it doesnt work for first where i want to replace “(stringified) object keys” with ‘’

It looks like quotes in stringified JSON are double quotes.

Replace ' with " in your regex.

1 Like