簡體   English   中英

如何將嵌套json屬性的值分配給父對象?

[英]How to assign value of nested json property to parent object?

說我有看起來像這樣的json:

"data": [{
        "name": "United States Department of Homeland Security",
        "TotalCount": 0,
        "UniqueCount": 3,
        "AveragedEntry": {
            "text": "United States Department of Homeland Security",
            "relevance": 0.20341,
        },
        "WeightedEntry": {
            "text": "United States Department of Homeland Security",
            "relevance": "NaN",
        }
}]

我想要的是一個看起來像這樣的對象的json數組:

"data": [{
            "name": "United States Department of Homeland Security",
            "TotalCount": 0,
            "UniqueCount": 3,
            "AveragedEntry": 0.20341,
            "WeightedEntry": "NaN"
    }]

在其中為父屬性AveragedEntry和WeightedEntry分配了相關屬性的值。

關於如何循環/創建/操作json對象,存在很多問題,但是我沒有遇到我要解決的特定問題。

是否可以使用javascript / jquery?

我試圖對我的初始json進行深層復制,並遍歷該對象,並在沒有運氣的情況下推送嵌套的對象。 任何幫助表示贊賞,謝謝。

 var test = { "data": [{ "name": "United States Department of Homeland Security", "TotalCount": 0, "UniqueCount": 3, "AveragedEntry": { "text": "United States Department of Homeland Security", "relevance": 0.20341, }, "WeightedEntry": { "text": "United States Department of Homeland Security", "relevance": "NaN", } }] }; //make a copy of the object var newThing = JSON.parse(JSON.stringify(test)); newThing.data.forEach(function(element){ //unnest the values that you want element.AveragedEntry = element.AveragedEntry.relevance; element.WeightedEntry = element.WeightedEntry.relevance; }); //original is not changed console.log(test); //new element exists console.log(newThing); 

使用Array.map

 let data = [{"name": "United States Department of Homeland Security","TotalCount": 0,"UniqueCount": 3,"AveragedEntry": {"text": "United States Department of Homeland Security","relevance": 0.20341,},"WeightedEntry": {"text": "United States Department of Homeland Security","relevance": "NaN",}}]; data = data.map(({AveragedEntry, WeightedEntry, ...rest}) => Object.assign(rest,{AveragedEntry : AveragedEntry.relevance, WeightedEntry : WeightedEntry.relevance})); console.log(data); 

您可以使用Array.map()將數組轉換為所需的結構:

 var data = [{ "name": "United States Department of Homeland Security", "TotalCount": 0, "UniqueCount": 3, "AveragedEntry": { "text": "United States Department of Homeland Security", "relevance": 0.20341, }, "WeightedEntry": { "text": "United States Department of Homeland Security", "relevance": "NaN", } }]; var res = data.map((obj)=>{ obj.AveragedEntry = obj.AveragedEntry.relevance; obj.WeightedEntry = obj.WeightedEntry.relevance; return obj; }); console.log(res); 

不指定AveragedEntryWeightedEntry

您可以使用map遍歷數組。 使用Object.entries將對象轉換為數組,並使用reduce制作新對象。

檢查值是否為對象,如果是,則使用relevance屬性。

 let data = [{"name":"United States Department of Homeland Security","TotalCount":0,"UniqueCount":3,"AveragedEntry":{"text":"United States Department of Homeland Security","relevance":0.20341},"WeightedEntry":{"text":"United States Department of Homeland Security","relevance":"NaN"}}]; //Loop each array element using `.map()` //variable o holds the array elements. let result = data.map(o => { //Use `Object.entries()` to convert each array element (object) into an array //This will return a multi dimensional array with element 0 the key and element 1 the value //Variable `k` is the key of the object. Like: name, AveragedEntry, WeightedEntry //Variable `v` is the corresponding value of the key. Like 0 or {"text": "United States Department of Homeland Security","relevance": 0.20341,} //If the `v` (value) is a type object, use the relevance and assign the value to the accumulator variable `c` return Object.entries(o).reduce((c, [k, v]) => { if (typeof v === 'object') c[k] = v.relevance; else c[k] = v; return c; }, {}); }); console.log(result); 

較短的版本:使用Object.assign向對象添加屬性和值。

 let data = [{"name":"United States Department of Homeland Security","TotalCount":0,"UniqueCount":3,"AveragedEntry":{"text":"United States Department of Homeland Security","relevance":0.20341},"WeightedEntry":{"text":"United States Department of Homeland Security","relevance":"NaN"}}] let result = data.map(o => Object.entries(o).reduce((c, [k, v]) => Object.assign(c, typeof v === 'object' ? {[k]: v.relevance} : {[k]: v}), {})); console.log(result); 

您可以使用Array.map函數為此類數據轉換創建管道。

 let data = [{ "name": "United States Department of Homeland Security", "TotalCount": 0, "UniqueCount": 3, "AveragedEntry": { "text": "United States Department of Homeland Security", "relevance": 0.20341, }, "WeightedEntry": { "text": "United States Department of Homeland Security", "relevance": "NaN", } }]; // Map the data into the new data structure. var mapped = data.map(e => ({ name: e.name, totalCount: e.TotalCount, uniqueCount: e.UniqueCount, averagedEntry: e.AveragedEntry.relevance, weightedEntry: e.WeightedEntry.relevance })); console.log(mapped) 

與使用JavaScript元編程api的某些答案相比,這可能有點羅word,但是我認為簡單性對於可讀性是最好的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM