如何将对象数组的所有属性设置为 true?
How can I set all the properties of an array of objects to true?
我有一些 JSON 数据,我正在尝试映射并将所有属性设置为 true。
我知道您可以为此使用 map 函数,但是 JSON 的设置方式是,数组中的每个对象都有一个键名。
所以当我 运行 映射函数时,它将所有键名设置为 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
}
}
这里是我尝试的map函数,其中data
指的是上面的JSON:
Object.entries(data).map(([key, value]) => {
return value === true;
});
这又是 returns 一个包含值 false
的五个项目的数组:
[false,false,false,false,false]
我正在尝试做的是将任何错误的项目的值翻转为正确的。
你不需要映射任何东西
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
}
}
使用递归:
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);
根据您的 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);
您也可以简单地使用嵌套的 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);
我有一些 JSON 数据,我正在尝试映射并将所有属性设置为 true。
我知道您可以为此使用 map 函数,但是 JSON 的设置方式是,数组中的每个对象都有一个键名。
所以当我 运行 映射函数时,它将所有键名设置为 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
}
}
这里是我尝试的map函数,其中data
指的是上面的JSON:
Object.entries(data).map(([key, value]) => {
return value === true;
});
这又是 returns 一个包含值 false
的五个项目的数组:
[false,false,false,false,false]
我正在尝试做的是将任何错误的项目的值翻转为正确的。
你不需要映射任何东西
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
}
}
使用递归:
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);
根据您的 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);
您也可以简单地使用嵌套的 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);