0

I tried this one

JSON.stringify(localStorage, function(key, value) {
        console.log(key);
        return (key.split('.')[0] === 'SUWDdb') ? value : undefined;
})

and it only returns undefined, console only log one entries. can any one give me a reason and a solution?

0

2 Answers 2

1

Well, nothing at the top level satisfies your condition, and in that case your function is returning undefined, which means nothing below that is stringified. You probably want:

JSON.stringify(localStorage, function(key, value) {
    console.log(key);
    return (typeof value === 'object' || key.split('.')[0] === 'SUWDdb') ? value : undefined;
            ^^^^^^^^^^^^^^^^^^^^^^^^^
})

This will allow JSON.stringify to keep traversing downward when it encounters an object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much dude, I use your approach and it works!!! You have just saved my life
0

Just keep it simple if you have localstorage and you want to stringify it use

JSON.stringify(localstorage) Where localstorage is variable that has localstorage object

jsonString = JSON.stringify(value [, replacer [, space]])

value The JavaScript object to convert into a JSON string.

replacer A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

This is the syntax of json.stringify function,its not a callback function

2 Comments

What does this mean for his use case where he wants to omit properties whose keys start with SUWDdb.?
Var result= JSON.stringify(localstorage)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.