繁体   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