JSFiddle - React, Tailwind, and code Playground
JavaScript
obj = {
"$id": "1",
"AppViewColumns": [
{
"$id": "2",
"AppView": {"$id":"1"},
"ColumnID": 1,
}
]
}
_.mixin({
relink: function (g, prop) {
prop = prop || '$id';
var ids = {};
function relink (s) {
// we care naught about primitives
if (s === null || typeof s !== "object") { return s; }
var id = s[prop];
delete s[prop];
// either return previously known object, or
// remember this object linking for later
if (ids[id]) {
return ids[id];
}
ids[id] = s;
// then, recursively for each key/index, relink the sub-graph
if (s.hasOwnProperty('length')) {
// array or array-like; a different guard may be more appropriate
for (var i = 0; i < s.length; i++) {
s[i] = relink(s[i]);
}
} else {
// other objects
for (var p in s) {
if (s.hasOwnProperty(p)) {
s[p] = relink(s[p]);
}
}
}
return s;
}
return relink(g,prop);
},
resolve: function(obj,prop){
prop = prop || '$id';
var objs = [];
function resolve (s) {
// we care naught about primitives
if (s === null || typeof s !== "object") { return s; }
replacementObj={};
if(objs.indexOf(s)!=-1){
replacementObj[prop]=objs.indexOf(s);
return replacementObj;
}
// then, recursively for each key/index, relink the sub-graph
if (s.hasOwnProperty('length')) {
// array or array-like; a different guard may be more appropriate
for (var i = 0; i < s.length; i++) {
s[i] = resolve(s[i]);
}
} else {
objs.push(s);
s[prop] = objs.indexOf(s);
// other objects
for (var p in s) {
if (s.hasOwnProperty(p)) {
...