[英]How to assign value of nested json property to parent object?
Say I have json that looks like this: 说我有看起来像这样的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",
}
}]
What I want is an json array of objects that looks like: 我想要的是一个看起来像这样的对象的json数组:
"data": [{
"name": "United States Department of Homeland Security",
"TotalCount": 0,
"UniqueCount": 3,
"AveragedEntry": 0.20341,
"WeightedEntry": "NaN"
}]
Where the parent properties AveragedEntry and WeightedEntry are assigned the value of the relevance property. 在其中为父属性AveragedEntry和WeightedEntry分配了相关属性的值。
There are a A LOT of questions about how to loop through / create / manipulate a json object but I have not come across this particular problem I want to solve. 关于如何循环/创建/操作json对象,存在很多问题,但是我没有遇到我要解决的特定问题。
Is this possible using javascript/jquery? 是否可以使用javascript / jquery?
I have tried making a deep copy of my initial json and looping through the object and pushing the nested object with no luck. 我试图对我的初始json进行深层复制,并遍历该对象,并在没有运气的情况下推送嵌套的对象。 Any help is appreciated thanks. 任何帮助表示赞赏,谢谢。
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);
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);
You can use Array.map()
to transform the array to desired structure: 您可以使用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);
Without specifying AveragedEntry
and WeightedEntry
, 不指定AveragedEntry
和WeightedEntry
,
You can loop thru the array using map
. 您可以使用map
遍历数组。 Use Object.entries
to convert the object into an array and use reduce
to make a new object. 使用Object.entries
将对象转换为数组,并使用reduce
制作新对象。
Check if the value is an object, if it is, use the relevance
property. 检查值是否为对象,如果是,则使用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);
Shorter Version: Use Object.assign
to add property and value to an object. 较短的版本:使用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);
You can use the Array.map function to create a pipeline for data transforms like this. 您可以使用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)
This might be a bit more wordy that some of the answers that use the JavaScript meta-programming api, but I think simplicity is best for readability. 与使用JavaScript元编程api的某些答案相比,这可能有点罗word,但是我认为简单性对于可读性是最好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.