Identifying & Printing an object within an object - Angular - javascript

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>

Related

Is there a way to select a key by using a string?

I am using the Nebular Angular theme. when my object is returned in the object ServerDataSource, you have to supply a key to get that string. However, in my
case, an object is returned and I need to try and select that string by selecting first the object and then the string.
I've already tried solutions such as "dataKey: 'data.page.maximum'" and so on.
this.tableBulkProductsOverviewRemoteSource = new ServerDataSource(this.httpClient, {
endPoint: 'URL',
dataKey: 'data',
totalKey: 'data.page.total',
pagerPageKey: ''
});
In the total key, I am trying to get the object and then the string via a string.
Is there any way around this?

Check if a string is contained within a specific Map() key

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.

Setting object data in nested for loop

I have a scope variable called jobs which is an array of objects for each department and their data.
[
“Accounting”: {
“foo” : “foo”,
“bar” : bar"
},
“Delivery”: {
“foo”: “foo”,
“bar”: “bar
}
]
The HTML5 date input requires that all dates be converted to using new Date() in javascript even though they are in the correct yyyy-mm-dd format specified. I could go through by hand and type in each date that needs to be converted, but I wanted to use a nested foreach to do so because I have a lot of dates to convert.
angular.forEach($scope.job, function(value, key){
angular.forEach(value, function(innerValue, innerKey){
if(typeof(innerValue) === "string"){
var matches = innerValue.match(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/);
if(matches){
// Need to set $scope.job.key.innerKey = new Date($scope.job.key.innerKey) here
}
}
});
});
My issues is that I don’t know how to access the $scope.job object at the key and innerKey values I came to. How can I edit the current item being looped over? The documentation on using ‘this’ in a situation like this was almost impossible to find.
You just need to use [] object notation for variable property names
if(matches){
value[innerKey] = new Date(value[innerKey]);
}
Another approach would be to make a directive wrapping the date input and put the conversion logic in the controller of that directive. This way all the looping would be implicitly done by the framework, for all instances of the custom date input directive.

Sort Json Array with LoDash

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

Why can't I access JSON objects by key in JavaScript?

This is my JSON object:
{ text: 'stuff',
user: 'user1
}
when I run a typeof jsonObj, I get object. When I run an jsonOb.length, I get undefined. When I tried to access the text property via console.log(jsonObj.text), I get undefined.
So how can I properly access everything in JavaScript?
I don't want to use jQuery as this is all node.js programming so it's serverside.
UPDATED - full JSON
{ text: '#junk_666 おかえりか',
user:
{ display_name: 'mono',
screen_name: 'monochrm',
klout_score: null,
location_str: '画面の前',
utc_offset: '32400' },
venue_id: 1304409836517,
match_type: 'twitter',
tweet_id: '116494264137023489',
created_at_unix: 1316609371,
meta:
{ matchedPhrase: 'junk',
venueName: 'deletemepls really long name' },
tags: [ '' ],
indexed_at_unix: 1316609416 }
The json seems to be invalid
{
"text": "stuff",
"user": "user1"
}
I copied and pasted your object into a FireBug console and it recognized it.
If you need to count the number of key/value pairs, you can use a function such as this one to do it:
function numMembers(o) {
var i=0;
for (a in o) {
i++;
}
return i;
}
You should be able to access the value of the text property via jsonObj.text. Are you sure that your object is being referenced by jsonObj? Also, can you access the values for simpler objects such as the ones mentioned in other posts if you create them? Furthermore, does anything work if you use only ASCII characters? For some reason, it might not be handling some of the non-Latin characters properly.
First, what you have is not JSON because JSON requires property name to be in double quotes. It is a valid JavaScript object literal though, so I'll assume that's how you're using it.
Secondly, JavaScript objects in general do not have a length property. Arrays do.
There's no problem with your object literal so there must be some other problem elsewhere in your code.
Try this:
{ text: 'stuff',
user: 'user1'
}
You left off an apostrophe.
Now that you've posted your full JS code (that's not JSON, as #josnidhin points out)... works fine in jsFiddle. http://jsfiddle.net/Rs9R4/ I don't believe a JS Object has .length, just Arrays.

Categories

Resources