How do I console log props to a JSON file? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm building a multi-step form in React and I would like to console.log the JSON of the props at the end with all the inputted information. How would I do this?
Here is my code:
I tried the deconstructed props in the arrow function component but it didn't work.
const Final = ({
values: { one, two... }
}) => {
console.log(JSON.stringify(Final);
I managed to log the info this way, but it's not exactly what I would like:
console.log(
"Value One: " + one,
"Value Two: " + two,
....);
I deleted my previous question by accident

I would probably just take props as an argument rather than destructuring:
const Final = (props) => {
console.log(JSON.stringify(props));

Related

How load two value via one parameter in function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
is it possible to read two different values over one parameter in a function? I need to load two values in one parameter into a function.
Thank you for your help
Yes, it's possible. Simplest example:
function test(params) {
return params.first + params.second;
}
console.log(test({
first: 3,
second: 4
}));
If you care about the names, then:
function test(params) {
var {first, second} = params;
return first + second;
}
console.log(test({
first: 3,
second: 4
}));

Converting a javascript string into an existing javascript variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];

Converting JSON with leaves and nodes to HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string that looks like the one below – this is straight from the DB:
"{\"object\":\"value\",\"document\":{\"object\":\"document\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"paragraph\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"here is some text\",\"marks\":[]}]}]}]}}"
This is the first time I see something like this. I can surely JSON.parse it but this leaves me with an object.
Is it a common thing to see somthing like this?
Is there a library for rendering this to HTML or do I have to write my own method to do this?
If the json structure is fixed, you can do something like this:
const json = JSON.parse("{\"object\":\"value\",\"document\":{\"object\":\"document\",\"data\":{},\"nodes\":[{\"object\":\"block\",\"type\":\"paragraph\",\"data\":{},\"nodes\":[{\"object\":\"text\",\"leaves\":[{\"object\":\"leaf\",\"text\":\"here is some text\",\"marks\":[]}]}]}]}}");
const leaves = [];
json.document.nodes.forEach(
n => n.nodes.forEach(
node => node.leaves.forEach(
leaf => leaves.push(leaf.text)
)
)
);
console.log(leaves);

Keep frozen object as global state [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am playing around with immer.js. Immer.js lock obejct after giving new instance. Is it ok to use this locked object as global state?
windows.initialState = {a: 'a'};
const nextState = produce(initialState , draftState => {
draftState.a = 'b',
});
windows.initialState = nextState;
Yes, you can assign and keep the frozen object to the global state. As long as your global object(initial state) is not declared as const. So, Nothing wrongs with this code.

How to "push" multiple values on one key in JavaScript Map [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi everyone i have the following problem. I have Map() that holds keys - System names, and values - nested Map() with key/value pairs component name/sub component name. I want to store multiple sub component names on one component name in the nested map. I try something like this when i get the same systemName and componentName in my code, but it didn't work.
mapName.get(systemName).get(componentName).set(subcomponentName, value);
You can extend Map to provide automatic nesting:
class NestedMap extends Map {
get(key) {
if(!this.has(key))
this.set(key, new NestedMap);
return super.get(key);
}
}
//
let myMap = new NestedMap;
myMap.get('systemName').get('componentName').set('subcomponentName', 'xyz');
console.log(myMap.get('systemName').get('componentName').get('subcomponentName'))

Categories

Resources