2

I have recursivity problem when trying to stringify an object.

I wonder if anyone have a tip to know what is the current depth when iterating trough key -> value in the replacer ? I want to replace a reference by its ID, but only when I'm at a specific depth.

Here is a minimalist object to give an exemple :

var a = {
  id: 'idOfA',
  linkedItems: []
};

var b = {
  id: 'idOfB',
  linkedItems: []
};

var c = {
  id: 'idOfC',
  linkedItems: []
};

a.linkedItems.push({
  from: 0,
  to: 0,
  item: b
});
b.linkedItems.push({
  from: 0,
  to: 0,
  item: a
}, {
  from: 0,
  to: 0,
  item: c
});
c.linkedItems.push({
  from: 0,
  to: 0,
  item: a
}, {
  from: 0,
  to: 0,
  item: b
});

var obj = {
  things: {},
  otherThings: {},
  items: [a, b, c]
};

From obj, I need to replace each references in the "linkedItems" attribute, by their id. I tough the easier way was to stringify it first ... :)

Alann S.
  • 125
  • 12

3 Answers3

0

I will assume what you call "references" (a ,b & c) are String .

var a="SA", b="SB", c="SC" ;

var obj = {
  things: {},
  otherThings: {},
  items: {
    a: {
      _id: 'idOfA',
      linkedItems: [b, c]
    },
    b: {
      _id: 'idOfB',
      linkedItems: [a]
    },
    c: {
      _id: 'idOfC',
      linkedItems: [a, b]
    }
  }
}
function getID(attr){
   var mapping={'SA':'a','SB':'b','SC':'c'};
   return obj.items[mapping[attr]]._id;
   

};

Object.keys(obj.items).map((value, index) =>{
  obj.items[value].linkedItems = obj.items[value].linkedItems.map((e)=>getID(e));
});

console.log(obj)
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • Sorry, but I don't see any possible way to adapt this to my needs :s. You can check my first post for more details :) – Alann S. Jul 29 '16 at 21:37
0

I finally found a way. But I'm not sure it's the optimal way to do it :s.

here is what I added :

Object.defineProperty(Object.prototype, 'map', {
    value: function (f, ctx) {
        ctx = ctx || this;
        var self = this;
        var result = {};
        Object.keys(self).forEach(function (k) {
            result[k] = f.call(ctx, self[k], k, self);
        });

        return result;
    }
});

var okayItems = obj.items.map(function (item, index) {
    item = Object.assign({}, item, {
        linkedItems: item.linkedItems.map(function (linkedItem) {
            linkedItem = linkedItem.map(function (value, key) {
                return (key === 'item') ? value.id : value;
            });

            return linkedItem;
        })
    });

    return item;
});
Alann S.
  • 125
  • 12
-1

You can implement the toJson function into the object of yours, and handle this case within it, then when you call JSON.stringify it will be returned from that object.

Difference between toJSON() and JSON.Stringify()

Community
  • 1
  • 1
MoustafaS
  • 1,991
  • 11
  • 20
  • I already tried this :). The problem is that it will always return the id even on items which aren't deep enough in my object. – Alann S. Jul 29 '16 at 19:56