How to Remove Empty list Elements from nested List [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 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.

Related

Filtering an array with an empty array [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 months ago.
Improve this question
const arrFilter = [{id:1},[],[]];
how can I filter the empty array in the above example, so that the result would be the object with an id only
You can use the length of the array returned from Object.keys() to determine if objects, arrays or strings are empty however it won't work on numbers.
With that in mind, try this assuming that
all empty objects, arrays and strings should be omitted
everything else stays
const arrFilter = [{id:1},[],[], "a string", "", 1, 0];
const nonEmpties = arrFilter.filter(
(item) => typeof item === "number" || Object.keys(item).length > 0
);
console.log(nonEmpties);

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.

Returning object property from array [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm using the filter method to return an object property when a value is true. However my filter method doesn't stop iterating over the array when it finds the true value so iterates over all the elements and returns a null value.
I want to break out of the array filter once the condition is true.
This is what I've done:
array.filter((v)=>{
var a = v.id === x ? v.collection : null
console.log(a)
return a
})
I have three elements in the array and my console.log prints out 'music', 'null', 'null'. I want it to break when it is 'music'.
If you only want the first matching element, you can use find rather than filter.
It will return the first matching element, stop iterating, and return the match.

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'))

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