2017-02-05 2 views
0

Wie würde ich ein Objekt wie unten dargestellt angehen, wenn ich alle Werte in der Hierarchie nach oben verschiebe, bis das übergeordnete Element nicht mehr _ignore heißt?Verschieben von JSON-Werten in einer verschachtelten Hierarchie, bis der Schlüssel nicht mehr '_ignore' heißt

In einem Versuch, Excel CSV-Daten auf ein verschachteltes JSON-Objekt zu verwandeln, landete ich mit etwas, das wie folgt aussieht:

// Old structure: 
var obj = { 
    _root: {}, 
    _top1: { 
     "_ignore": { 
      "_ignore": [ 
       "I can no longer traverse down", 
       "so the list that contains this", 
       "text should be placed under the", 
       "_top1 node, which is the first", 
       "parent not named _ignore. It should", 
       "be the only child to '_top1'." 
      ] 
     } 
    }, 

    _top2: {} 
} 

_ROOT und _top2 sollen nicht berührt werden, aber die top1 das haben sollte Liste der niedrigsten Ebene als Wert in der endgültigen Struktur. Ich möchte das Objekt wie folgt aussehen:

// Desired structure: 
var obj = { 
    _root: {}, 
    _top1: [ 
      "I can no longer traverse down", 
      "so the list that contains this", 
      "text should be placed under the", 
      "_top1 node, which is the first", 
      "parent not named _ignore" 
      ], 

    _top2: {} 
} 

Ich weiß, dass ich in der rekursiven Domain bin, sind gerade nicht in der Lage gewesen, es in geeigneter Weise anzuwenden. Hilfe/Richtung wird sehr geschätzt.

Antwort

1

der Tat wollen Sie im Grunde nur den Inhalt der tiefste _ignore. Da Sie auch alle Vorgängerobjekte (mit Ausnahme von Obj selbst) in Arrays konvertieren, können Sie sicher sagen, dass alle anderen Eigenschaften zerstört werden können. Mit anderen Worten, ein Objekt hat entweder eine _ignore Eigenschaft oder ist der eigentliche Inhalt, den wir suchen.

In einem Satz könnte man sagen; gib mir den Inhalt von _ignore wenn es gibt, und rekrutieren Sie das.

In Pseudo-Code:

function findContents (level) { 
    if (i have an ignored level) 
     return findContents (ignored level) 
    else 
     return level 
} 

In Javascript-Code:

const findContents = obj => obj._ignore 
    ? findContents(obj._ignore) 
    : obj; 

Und das auf Ihre Struktur anzuwenden:

obj._top1 = findContents(obj._top1); 

Viel Spaß

1

Vielleicht kann diese Funktion Ihnen helfen:

const removeIgnore = (obj) => { 

    let newObj = Object.assign({}, obj); 

    const findStructure = (obj) => { 
     const keys = Object.keys(obj) 
     if (keys.length == 0) return obj 
     return (keys[0] === '_ignore') ? findStructure(obj[keys[0]]) : obj 
    } 

    for (k in newObj) newObj[k] = findStructure(newObj[k]) 

    return newObj 
} 

Es werden alle Root-Schlüssel iterieren und für die tiefste Struktur suchen, die nicht Teil eines _ignore Schlüssel. Gibt ein neues Objekt mit den geänderten Daten zurück.

Demo:

/* 3 deeps _ignore */ 
var obj = { 
    _root: {}, 
    _top1: { 
     "_ignore": { 
      "_ignore": { 
       "_ignore": [ 
        "I can no longer traverse down", 
        "so the list that contains this", 
        "text should be placed under the", 
        "_top1 node, which is the first", 
        "parent not named _ignore. It should", 
        "be the only child to '_top1'." 
       ] 
      } 
     } 
    }, 

    _top2: {} 
} 


const newObj = removeIgnore(obj); 

console.log(newObj); 

/* 
{ _root: {}, 
    _top1: 
    [ 'I can no longer traverse down', 
    'so the list that contains this', 
    'text should be placed under the', 
    '_top1 node, which is the first', 
    'parent not named _ignore. It should', 
    'be the only child to \'_top1\'.' ], 
    _top2: {} } 

*/ 
1

Sie eine Tiefe tun müssen, um zuerst den Baum, um die ignoriert zu ersetzen Suche nach unten

var obj = { 
 
    _root: {}, 
 
    _top1: { 
 
     "_ignore": { 
 
      "_ignore": [ 
 
       "I can no longer traverse down", 
 
       "so the list that contains this", 
 
       "text should be placed under the", 
 
       "_top1 node, which is the first", 
 
       "parent not named _ignore. It should", 
 
       "be the only child to '_top1'." 
 
      ] 
 
     } 
 
    }, 
 

 
    _top2: {} 
 
} 
 
function hoistIgnore(item, parent, parent_key){ 
 
    if(Array.isArray(item) || !(typeof item === "object")) { 
 
    parent[parent_key] = item; 
 
    return item 
 
    } 
 
    for(var key in item){ 
 
    if(key === "_ignore"){ 
 
     hoistIgnore(item[key], parent, parent_key); 
 
    } else { 
 
     hoistIgnore(item[key], item, key); 
 
    } 
 
    } 
 
    return item 
 
} 
 

 
console.log(hoistIgnore(obj))

Verwandte Themen