const myArray = ['manjula','26','Software Engineer'] myArray[2] = myArray const myArray1 = [ 1 , 'a' , { data : 'manjula'}] myArray1[2].parent = myArray1 function encodeCycles(input) { if(typeof input !== 'object') { return input } const map = new WeakMap() map.set(input, '$') function encodeJSONCycle(obj, cleanedObj = {}, path = '') { if(typeof obj !== 'object') { return obj } for(var key in obj) { if(key && typeof obj[key] === 'object') { if(!map.get(obj[key])) { map.set(obj[key], `${path}.${key}`) cleanedObj[key] = encodeJSONCycle(obj[key], cleanedObj[key], `${path}${key}`) } else { cleanedObj[key] = { '$ref' : map.get(obj[key]) } } } else { cleanedObj[key] = obj[key] } } return cleanedObj } return encodeJSONCycle(input, input instanceof Array ? [] : {}) } JSON.encodeCycles = encodeCycles console.log('original input : ' , myArray ,'\nfinal output : ', JSON.encodeCycles(myArray)) console.log("Stringify" + JSON.stringify(JSON.encodeCycles(myArray),'\t',4)) console.log('original input : ' , myArray1 ,'\nfinal output : ', JSON.encodeCycles(myArray1)) console.log("Stringify" + JSON.stringify(JSON.encodeCycles(myArray1),'\t',4)) console.log(JSON.encodeCycles(100))