我尝试映射一些JSON数据并将所有属性设置为true。
我知道您可以为此使用map函数,但是通过设置JSON的方式,数组中的每个对象都有一个键名。
因此,当我运行map函数时,它将所有键名都设置为false,而不是对象本身内部的属性。
这是JSON数据:
{
"Presentations": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 5
},
"Articles": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 1
},
"Blogs": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 61
},
"NewsBriefs": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 2
},
"SpecialInsights": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 50
}
}
这是我尝试的地图函数,其中data
指的是上面的JSON:
Object.entries(data).map(([key, value]) => {
return value === true;
});
这反过来将返回一个包含五个项的数组,其值为false
:
[false,false,false,false,false]
我试图做的是将任何错误的值翻转为true。
答案 0 :(得分:1)
您也可以像这样简单地使用嵌套的forEach-
const a = {
"Presentations": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 5
},
"Articles": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 1
},
"Blogs": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 61
},
"NewsBriefs": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 2
},
"SpecialInsights": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 50
}
}
Object.keys(a).forEach(ele => {
Object.keys(a[ele]).forEach(childEle => {a[ele][childEle] = true});
})
console.log(a);
答案 1 :(得分:0)
您不需要映射任何内容
var arr = your_json;
for(a in arr){
arr[a]['Instant'] = true;
arr[a]['Daily'] = true;
arr[a]['WeeklySummary'] = true;
}
如果您不知道属性
for(a in arr){
for(e in arr[a]){
!arr[a][e] ? arr[a][e] = true : null
}
}
答案 2 :(得分:0)
使用递归:
function flip(o) {
for (k of Object.keys(o)) {
if (typeof o[k] === "object") {
flip(o[k])
}
if (o[k] === false) {
o[k] = true;
}
}
}
const o = {
"Presentations": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 5
},
"Articles": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 1
},
"Blogs": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 61
},
"NewsBriefs": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 2
},
"SpecialInsights": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 50
}
}
flip(o);
console.log(o);
答案 3 :(得分:0)
根据您的ES版本和所需的浏览器支持,这里有两种不同的处理方法。
var data = {
"Presentations": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 5
},
"Articles": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 1
},
"Blogs": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 61
},
"NewsBriefs": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 2
},
"SpecialInsights": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 50
}
};
/* ES5 */
var outterKeys = Object.keys(data);
for(var o = 0; o < outterKeys.length; o++){
var outterObject = data[outterKeys[o]];
var innerKeys = Object.keys(outterObject);
for(var i = 0; i < innerKeys.length; i++){
var innerObject = outterObject[innerKeys[i]];
if (!innerObject){
data[outterKeys[o]][innerKeys[i]] = true;
}
}
}
console.log('es5', data);
/* ES6 */
data = {
"Presentations": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 5
},
"Articles": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 1
},
"Blogs": {
"Instant": true,
"Daily": false,
"WeeklySummary": true,
"ContentTypeId": 61
},
"NewsBriefs": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 2
},
"SpecialInsights": {
"Instant": false,
"Daily": false,
"WeeklySummary": false,
"ContentTypeId": 50
}
};
Object.keys(data).forEach((o) => {
Object.keys(data[o]).forEach((i) => {
if (!data[o][i]){
data[o][i] = true
}
})
});
console.log('es6', data);