I have a map which i am populating from a json file, similar to the below
key: ConfigItem
value: Var1,Var2,Var3
key: ConfigItem2
value: Var1,Var2,Var3,var4
key: ConfigItem3
value: true
i want to be able to run an if statement to check if a value is contained within the "ConfigItem" key, and if so, do something.
I looked at map.get and map.has but i can't seem to be able to figure out how to search a specific key and return if it contains a specific value.
You seem to be looking for
const map = new Map([
["ConfigItem", ["Var1","Var2","Var3"]],
["ConfigItem2", ["Var1","Var2","Var3","var4"]],
["ConfigItem3", true],
]);
if (map.get("ConfigItem").includes("Var2")) {
…
}
Notice this just uses Map get to access the array value for the ConfigItem key, but then uses the normal Array includes method to check if the specific string is contained in it.
Related
I'm new to JavaScript and I want a Data Structure for my JavaScript code which stores Student data as key value pairs. The key is the student Registration number and the value is the students name.What I'm thinking is to create a JavaScript object as follows and store data as follows
let Student={
001A:"John",
002A:"Mathew"
};
Is this approach is correct? And if it is correct suppose a way to dynamically add key value pairs to that. Thank you
That would be an object literal. You'd want the key to be a string, but other than that you've basically got it. You can dynamically add properties using bracket syntax. I've attached a small snippet to give you an idea of how it works. :)
let Student={
"001A":"John",
"002A":"Mathew"
};
Student["003A"] = 'Jessica';
Object.entries(Student).forEach(entry => console.log(entry) );
The approach is correct. Given
const students={
'001A': 'John',
'002A': 'Mathew',
};
(Note: It's a good idea to keep your key as a string to prevent collisions with reserved keywords)
To read from the structure you can access the given record via
console.log(students['001A']); // => 'John'
To add new records dynamically, you just add a new property and assign it the desired value:
students['007'] = 'Ben';
which results in
console.log(students);
// =>
{
'001A': 'John',
'002A': 'Mathew',
'007': 'Ben',
};
To delete a given record you can do either
delete students['007'];
or
students['007'] = undefined;
The first option is a little "cleaner" as it completely deletes the given key, along with its assigned data.
Keep in mind that the data will be removed once you reload the page.
I have a JSON in the following format:
{
key: value,
key: value,
key: {
key: value,
key: value
}
}
I want to print these key values out to my page using Angular's ngIf to identify if the value is a string or another object. I tried using typeof but it gives me an unresolved variable error in Webstorm.
How do I know that the value is an object and that I need to go in and take key values from in there?
I am using Angular 6 & Webstorm IDE.
You can create a function to get that value.Take a look at this demo
In component.ts:
checkType(data){
return typeof(data)
}
In html:
<div *ngIf="checkType(data.k1) === 'string'">
I am string
</div>
I've been trying to access a third level node in an array using the indexes in it, but I can't access it, I tried a lot of ways that I found here on SO but I don't want to iterate through it, I want to get it manually.
var data = [
{code:1,
label:'John Doe',
tasks:[{
code:1,
label: 'AnyProject',
starts:'2016/1/25',
ends:'2016/2/25'}]
}];
What I want to do (theoretically):
data[0].tasks.code
data[0].tasks[0].code
tasks is an Array so you need to access it like an array.
data[0].tasks[0].code
Inside data array you have tasks and inside task array you have property code.
[] is an array you can use index to look inside.
{} is an object you can access using .
I need to be able to store a collection of items in javascript. I need to be able to delete them by key. This is a crud application. What's the best way to do this?
Do I need to remove the item with a splice from a standard array and then update the array to remove the missing space?
From your question is sounds like you are trying to do this using an array. It would be a lot easier to use an object as a key-value store. E.g.,
var collection = {};
Then, to add an item with a key:
collection[key] = item;
To get an item:
var item = collection[key];
And to delete an item:
delete collection[key];
If you want things to be easy, it is important to use a strings for keys. A key will be implicitly converted to a string using .toString(). This does not yield a useful result (unless overridden) if you try using an object as a key because it will be converted to the string "[object Object]".
I have a JSON array whose general structure is like this:
var json = [
{ key: 'firstName', value: 'Bill' },
{ key: 'lastName', value: 'Mans' },
{ key: 'phone', value: '123.456.7890' }
];
In reality, there will be a lot more key/value pairs. Either way, I'm trying to sort this array by the key value using Lodash. Currently, I'm trying the following:
_.map(_.sortBy(json, key), _.values);
However, that causes an error that says:
[ReferenceError: key is not defined]
I suspect its because key is not wrapped in quotes as shown in the docs. Unfortunately, I do not actually have control over the format of the json. In reality its being used by other developers and I'm the last to use it. Is there a way for me to sort the json array, by key names, using lodash? If so, how?
thank you
You need to put the key in quotes only when calling sortBy. It doesn't have to be in quotes in the data itself.
_.sortBy(json, "key")
Also, your second parameter to map is wrong. It should be a function, but using pluck is easier.
_.pluck( _.sortBy(json, "key") , "value");
_.map(_.sortBy(json, 'key'), 'value');
NOTE: In the latest version of lodash, _.pluck no longer exists. Use _.map instead as a drop-in replacement