-1

I have this code

car = {
    "car_id": "325SDGSD-GSH4GRWW-DH534R",
    "Name": "suzuki",
    "desc": "compact machine to get your from point A to B",
    "engineSerial": "305-6467-3674",
    "Country": "Netherlands",
    "Province": "George",
    "City": "Charlie",
    "branch_timings": {
        "Monday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Tuesday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Wednesday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Thursday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Friday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Saturday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Sunday": {
            "doorsOpen": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "03:00" }
            ]
        }
    },
    "status": true,
    "specific_timings": true,
    ]
};

I want to turn all the timingsEnabled from true to false at once.

what I have tried

I found a similar thread here

The following snippet is what was in the post and it works.. I tried implementing it in my own code and it does not seem to work, I shall include a snippet with my own code below.

array1 = [{
    name : "users",
    checked : true
  }, {
    name : "active users",
    checked : false
  }, {
    name : "completions",
    checked : false
  }]

  console.log(array1.map(function(x) { 
    x.checked = true; 
    return x
  }));

car = {
"car_id": "325SDGSD-GSH4GRWW-DH534R",
"Name": "suzuki",
"desc": "compact machine to get your from point A to B",
"engineSerial": "305-6467-3674",
"Country": "Netherlands",
"Province": "George",
"City": "Charlie",
"branch_timings": {
    "Monday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Tuesday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Wednesday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Thursday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Friday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Saturday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "00:00" }
        ]
    },
    "Sunday": {
        "doorsOpen": true,
        "timings": [
            { "timeOpen": "00:01", "timeClose": "01:00" },
            { "timeOpen": "12:00", "timeClose": "03:00" }
        ]
    }
},
"status": true,
"specific_timings": true,
]
};

As you can see it gives the error that locations.map is not a function. I would appreciate some feedback on what I am doing wrong or how I can fix this

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Hey, please do not completely change the title, body and topic of your question after you already received answers on it! This is *really bad style* as it leaves answers hanging in here that seem completely unrelated to the question. Please undo you latest changes and revert to the original question. After that start the current topic as a new question, but only *after* thoroughly researching that this *is* a new question and has not been asked before. The answer to the "new" question can be found here: https://stackoverflow.com/a/34087850/2610061 – Carsten Massmann Aug 20 '20 at 04:47
  • 1
    I've rolled the question back; it is inappropriate and against Stack Overflow policy to change questions after getting answers. Ask a new question or read the answers on the duplicate provided by @cars10m. – Heretic Monkey Aug 24 '20 at 12:13

6 Answers6

2

Try this:

for (var key in locations.branch_timings) {
        locations.branch_timings[key].timingsEnabled = false;
}
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29
  • how would I console.log this as logging it before a for loop doesn't work if I'm not wrong – SystematicallyWrongHa Aug 19 '20 at 21:21
  • Would this be possible with a while loop as well? And what would I need to console.log after that loop? I'm fairly new to JS and I'm having trouble understanding it, could you please explain? – SystematicallyWrongHa Aug 19 '20 at 21:28
  • @SystematicallyWrongHa Be clear, I do not understand what result you are looking for – Baruch Mashasha Aug 19 '20 at 21:30
  • I'm asking if your approach could be edited in such a way that instead of using the for loop or forEach, we use a while loop and also you asked me to console.log it after the for, what exactly would I be console.logging? – SystematicallyWrongHa Aug 19 '20 at 21:32
  • I have a restriction, I was able to do this before with forEach but I was asked to do it without for loop or forEach that is why I am unable to solve it, I do not understand how to incorporate a while loop into it – SystematicallyWrongHa Aug 19 '20 at 21:37
1

Here's another way to accomplish this:

Object.values(locations.branch_timings).forEach(day => day.timingsEnabled = false)
ksbrooksjr
  • 11
  • 3
1

@Baruch Mashasha already nailed it already with his answer. But here is another way of doing it:

Object.values(locations.branch_timings).forEach(o=>
  o.timingsEnabled && (o.timingsEnabled=false));

In my version I check whether the property timingsEnabled actually exists before setting it.

locations = {
    "location_id": "325SDGSD-GSH4GRWW-DH534R",
    "Name": "Earth",
    "Address": "Milky Way Galaxy, Alpha Quadrant, near Mars",
    "Phone": "111-111-111",
    "AddressContinue": null,
    "Zip": "73463",
    "Locality": "Testing location name",
    "Country": "Netherlands",
    "Province": "George",
    "City": "Charlie",
    "Latitude": "22.22222",
    "Longitude": "11.111111",
    "enable_store_timings": true,
    "branch_timings": {
        "Monday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Tuesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Wednesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Thursday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Friday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Saturday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Sunday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "03:00" }
            ]
        }
    },
    "status": true,
    "specific_timings": true,
    "delivery_areas": [
        "Earth Quadrant a",
        "Earth Quadrant b",
        "Earth Quadrant c",
        "Earth Quadrant d",
        "Earth Quadrant e"
    ]
};

Object.values(locations.branch_timings).forEach(o=>o.timingsEnabled && (o.timingsEnabled=false));

console.log(locations.branch_timings);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

your locations object is not an array, that's why it does not work. You want to map on the "branch_timings" property of your locations object, but it's not an array type neither. If you want to use the map() function you have to make this property an array.

Romka
  • 144
  • 8
0

branch_timings is an object, so you could iterate over its keys ("Monday", "Tuesday", etc). One approach to do this is getting the array of those keys using Object.key():

let locations = {
    "location_id": "325SDGSD-GSH4GRWW-DH534R",
    "Name": "Earth",
    "Address": "Milky Way Galaxy, Alpha Quadrant, near Mars",
    "Phone": "111-111-111",
    "AddressContinue": null,
    "Zip": "73463",
    "Locality": "Testing location name",
    "Country": "Netherlands",
    "Province": "George",
    "City": "Charlie",
    "Latitude": "22.22222",
    "Longitude": "11.111111",
    "enable_store_timings": true,
    "branch_timings": {
        "Monday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Tuesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Wednesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Thursday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Friday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Saturday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Sunday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "03:00" }
            ]
        }
    },
    "status": true,
    "specific_timings": true,
    "delivery_areas": [
        "Earth Quadrant a",
        "Earth Quadrant b",
        "Earth Quadrant c",
        "Earth Quadrant d",
        "Earth Quadrant e"
    ]
};

let branch_timings=locations.branch_timings;
for(let key of Object.keys(branch_timings)){
  branch_timings[key].timingsEnabled=false;
}

console.log(locations);
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • would there be a way to do this without using the for loop? would this be possible with a while loop as well? if so how would we go about using key in that – SystematicallyWrongHa Aug 19 '20 at 21:25
  • 1
    With a `while` loop you would need to use indexes, something like `let keys=Object.keys(...); let i=0; while(i – tevemadar Aug 19 '20 at 21:36
0

Welcome to [StackOverflow](https://stackoverflow.com]! We hope you like it here.

You can focus on locations.branch_timings, extract it's keys and use them to set the required property to false forEach of the keys:

//point to branch_timings
let b = locations.branch_timings;

Object.keys(b).forEach(key => !b[key].timingsEnabled || (b[key].timingsEnabled = false));

let locations = {
    "location_id": "325SDGSD-GSH4GRWW-DH534R",
    "Name": "Earth",
    "Address": "Milky Way Galaxy, Alpha Quadrant, near Mars",
    "Phone": "111-111-111",
    "AddressContinue": null,
    "Zip": "73463",
    "Locality": "Testing location name",
    "Country": "Netherlands",
    "Province": "George",
    "City": "Charlie",
    "Latitude": "22.22222",
    "Longitude": "11.111111",
    "enable_store_timings": true,
    "branch_timings": {
        "Monday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Tuesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Wednesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Thursday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Friday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Saturday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Sunday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "03:00" }
            ]
        }
    },
    "status": true,
    "specific_timings": true,
    "delivery_areas": [
        "Earth Quadrant a",
        "Earth Quadrant b",
        "Earth Quadrant c",
        "Earth Quadrant d",
        "Earth Quadrant e"
    ]
};

//point to branch_timings
let b = locations.branch_timings;

Object.keys(b).forEach(key => !b[key].timingsEnabled || (b[key].timingsEnabled = false));

console.log( locations );

Alternatively, you can use a for of loop on the values (as opposed to keys) of locations.branch_timings as follows:

for( let a of Object.values(locations.branch_timings) ) {
    !a.timingsEnabled || (a.timingsEnabled = false);
}

let locations = {
    "location_id": "325SDGSD-GSH4GRWW-DH534R",
    "Name": "Earth",
    "Address": "Milky Way Galaxy, Alpha Quadrant, near Mars",
    "Phone": "111-111-111",
    "AddressContinue": null,
    "Zip": "73463",
    "Locality": "Testing location name",
    "Country": "Netherlands",
    "Province": "George",
    "City": "Charlie",
    "Latitude": "22.22222",
    "Longitude": "11.111111",
    "enable_store_timings": true,
    "branch_timings": {
        "Monday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Tuesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Wednesday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Thursday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Friday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Saturday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "00:00" }
            ]
        },
        "Sunday": {
            "timingsEnabled": true,
            "timings": [
                { "timeOpen": "00:01", "timeClose": "01:00" },
                { "timeOpen": "12:00", "timeClose": "03:00" }
            ]
        }
    },
    "status": true,
    "specific_timings": true,
    "delivery_areas": [
        "Earth Quadrant a",
        "Earth Quadrant b",
        "Earth Quadrant c",
        "Earth Quadrant d",
        "Earth Quadrant e"
    ]
};

for( let a of Object.values(locations.branch_timings) ) {
    !a.timingsEnabled || (a.timingsEnabled = false);
}

console.log( locations );
PeterKA
  • 24,158
  • 5
  • 26
  • 48