简体   繁体   中英

How can I set all the properties of an array of objects to true?

I have some JSON data that I am attempting to map over and set all the properties to true.

I am aware that you can use the map function for this, however the way that the JSON is set up, each of the objects in the array has a key name.

So when I run the map function, it's setting all of the key names to false rather than the properties inside of the object itself.

Here is the JSON 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
    }
}

Here is the map function I attempted, where data refers to the JSON above:

Object.entries(data).map(([key, value]) => {
     return value === true;
});

This in turn returns an array of five items with the value false :

[false,false,false,false,false]

What I am attempting to do is flip the values of any items that is false to true.

You can simply do this using nested forEach as well, like this -

 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);

you dont need to map anything

var arr = your_json;
for(a in arr){
    arr[a]['Instant'] = true;
    arr[a]['Daily'] = true;
    arr[a]['WeeklySummary'] = true;
}

and if you dont know the properties

for(a in arr){
    for(e in arr[a]){
        !arr[a][e] ? arr[a][e] = true : null
    }
}

use recursion:

 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);

Here is a couple of different ways of doing it depending on your ES version and browser support required.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM