说我有一个看起来像这样的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进行深层复制,并遍历该对象并顺利地推送嵌套的对象。任何帮助表示赞赏,谢谢。
答案 0 :(得分:2)
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);
答案 1 :(得分:1)
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);
答案 2 :(得分:1)
您可以使用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);
答案 3 :(得分:1)
不指定AveragedEntry
和WeightedEntry
,
您可以使用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);
答案 4 :(得分:0)
您可以使用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,但是我认为简单性对于可读性是最好的。