How to "push" multiple values on one key in JavaScript Map [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 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'))

Related

How to Remove Empty list Elements from nested List [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
How can I remove Empty item such as Item A2[]
List = {[
item[{
A :"A",
A1:"C",
A2:[]
item B[{
B1: []
}]
]}
}]
Use the delete statement, it would go something like this:
if (obj[propName] === null || obj[propName] === undefined) {
delete obj[propName];
}
You can use that inside a for loop if you have an array of items.
This is an abstract way to delete keys with null or undefined values from an object since your code isn't really that much clear.
UPDATE:
To implement similar functionality in C#, you will have to use the .RemoveKey("Key") function.
So for example say you have the key as label inside an item object, use the following syntax:
item.RemoveKey("label"); or if it was in an array use: item[index].RemoveKey("label");
Hope this helped.

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

How do I console log props to a JSON file? [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'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));

How to create object inside object in JS [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 want to create an object inside an object in JavaScript like this:
{
"test": {
{},
{}
}
}
In more detail, I want to create a new object if "test" doesn't exist in the object, and create a new object in the same location as the old one if "test" exists.
Please help if this is possible.
You mean, like:
theObject["test"] = {};
this?
It's just an assignment, mate!
You need to give a key in every object you create in JavaScript.
E.g.:
var obj = {
objInside: {}
}
you can read more about object literals.

Remove an array-Javascript [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 8 years ago.
Improve this question
I have an array like this:
var animals=["cat","dog","snake","rhino"];
But sometimes I have to delete this array(remove it from the dom).
I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?
var animals=["cat","dog","snake","rhino"];
then to clear it do:
animals=[];
or
animals.length=0;
or
while(animals.length > 0) {
animals.pop();
}
Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.
Donot try to call delete operator that is explicit removal.
animals = undefined
OR
animals = void 0
Thanks
Just Clear The Array
Using This 2 Methods
animals.length =0
animals=[];

Categories

Resources