68,253 views
36 votes
36 votes
Use a replacer array of JSON.stringify to return only the year, month and day properties of jsonStructure, and assign stringData with the result.

var jsonStructure = { "year": 2011, "month": 8, "day": 3, "hour": 8, "minute": 49 };
/* Your solution goes here */

User Tylkonachwile
by
2.4k points

1 Answer

14 votes
14 votes

Final answer:

To filter the JSON object using JSON.stringify, a replacer array with 'year', 'month', and 'day' is created and passed to JSON.stringify, which results in a string containing only those date properties.

Step-by-step explanation:

The question asks how to utilize the JSON.stringify method with a replacer array to filter out certain properties from a JSON object, specifically retaining only the year, month, and day. To achieve this, you must define a replacer array containing the strings 'year', 'month', and 'day'. You will then pass this array as the second argument to JSON.stringify.

Here's the code snippet that accomplishes this:

var jsonStructure = { "year": 2011, "month": 8, "day": 3, "hour": 8, "minute": 49 };
var replacerArray = ['year', 'month', 'day'];
var stringData = JSON.stringify(jsonStructure, replacerArray);

This code will result in stringData being assigned a JSON string that contains only the properties 'year', 'month', and 'day' from jsonStructure.

User GabrielP
by
3.3k points