How to get the values from object? - javascript

I figured out how to make it in key value pair. But I tried getting values through different but none of them works.
This is how my map looks like. I am unable to get the values based on key.
Here is the code I am using
var storevalue = new Array();
storevalue = new Map(Object.entries(JSON.parse('{!filterModelstr}')));
Image shows what storevalue looks like.
cols.forEach(function(col) {
var filterInstance = gridOptions.api.getFilterInstance(col.getId());
// Set the model for the filter
filterInstance.setModel({
condition1: {
type: storevalue.get(col.getId()).storeTypeOperator,//Giving error at ths point
filter: storevalue.get(col.getId()).storefiltervalue,
},
operator: 'AND',
condition2: {
type: storevalue.get(col.getId()).storeTypeOperator,
filter: storevalue.get(col.getId()).storefiltervalue,
},
});
});
I am planning to assign values to type and filter which is not happening. How can I solve this scenario.

Hi I think what you are doing is pretty correct and should work provided your storevalue.get(col.getId()) method works and returns value object.
try to debug to see if your storevalue.get is giving correct json or not.
check out this plunker i made to replicate what you are trying to do.

Related

Array not emptying

Im trying to empty the array below but it it just keeps piling onto it instead of actually emptying it. Any ideas why this happens?
displaytaken[];
Edit: Adding a StackBlits:
Im getting way to confused. With the Snippet/JQ and im changing to much to get a accurate answer out of this.
Issue now is that it actually works in the stackblitz so something els is asstray will be editing it to reach a working recreation
https://stackblitz.com/edit/angular-ivy-kdec3f?
Another Edit:
Im finding it hard to recreate the issue but on the stacklits youll notice a function called checkinbetween() in there is a array called : filteredArray
This is storing data even if i clear it despite changing it from const to var so its duplicating all outputs on my side
Screenshot to help show whats happening on my side:
As u can see I clear the array but then when I add to it again the old values are there still
You are using Typescript, which gives you the added advantages of using types. Also, I would advice you to start using immutablility.
Instead of populating some 'global' array, you should create functions for you that return you the data that you need:
// Instead of this
const myDateArray: Date[] = [];
for (const someValue of someOtherDateArray) {
if (some.condition()) {
myDateArray.push(someValue);
}
}
// Do this:
function getDateArray(otherDateArray: Date[]): Date[] {
return otherDateArray.filter(date => some.condition());
}
That way, you don't need to clear the date array, as a new one can be requested on the fly.
To get to your answer, I assume you are looping over a lot of date arrays, and displaying them as taken dates in an alert box. here is how you can do that without clearing the array everytime:
const allTakenDates = [
[
new Date('2018-01-16'),
new Date('2018-03-26'),
new Date('2018-05-22'),
new Date('2018-12-01'),
new Date('2018-01-23'),
],
[
new Date('2019-01-16'),
new Date('2019-03-26'),
new Date('2019-05-22'),
new Date('2019-12-01'),
new Date('2019-01-23'),
],
[
new Date('2020-01-16'),
new Date('2020-03-26'),
new Date('2020-05-22'),
new Date('2020-12-01'),
new Date('2020-01-23'),
]
];
function getDatesAsString(dates: Date[]): string[] {
return dates.map(date => `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`);
}
for (const dates of allTakenDates) {
const formattedDates = getDatesAsString(dates).join();
alert('The following dates are taken: ' + formattedDates);
}
And here is a working example of that code.

How to add data to a 2 dimensional array in javascript

I am trying to add values into an array but for some reason it is not working. I am new to JavaScript.
Here is my code:
eventsArray = new Array();
$.each(xmlJsonObj.feed.entry, function(index, value){
eventsArray[index] = new Array('title' = value.title, 'date' = value.date[1]);
});
So basically I am pulling out some values from the json object and want to save them as key-value pairs in an array (multidimensional as each event has several values).
This array will later be sorted by date.
I am currently getting the following error:
ReferenceError: Left side of assignment is not a reference.
I am new to JavaScript and don't really understand whats wrong. Tried to look at some examples but still can't see a good example of creating two dimensional arrays with JavaScript (or objects, as everything in JS is an object) in a loop like this.
I would be very thankfull for any help or tips.
The cause of the error message is this:
'title' = value.title
That would mean that you are trying to assign a value to a literal string. The rest of the code (except from the other one just like it) is actually valid syntax, even if that is not what you are trying to do, so that's why you get the error message on that part of the code.
To have a collection of key-value pairs you would use an object instead of an array, and you can create it like this:
eventsArray[index] = { title: value.title, date: value.date[1] };
May be simplest one,
var eventsArray = new Array();
$.each(xmlJsonObj.feed.entry, function (index, value) {
eventsArray[index] = { 'title': value.title, 'date': value.date[1] };
});
It works if you change your code to:
var eventsArray = new Array();
$.each(xmlJsonObj.feed.entry, function(index, value){
eventsArray.push({ title : value.title, date: value.date[1] });
});
You should use objects for this:
eventsArray[index] = {};
eventsArray[index].title = value.title;
eventsArray[index].date = value.date[1];
The problem you have is that you try to assign value.title value to String. Arrays in JS didn't work in that way. Also Arrays didn't support String keys, this is why you may need Object.
If your date property is TIMESTAMP for example you can sort it like:
eventsArray.sort( function( a, b ) {
return a.date - b.date;
});

KnockoutJS, mapping plugin, get notified when changes in model?

Im using knockoutJS in following way:
var messagesFromServer = getJSONData().messages; //this get msgs from server
ko.mapping.fromJS(messagesFromServer, {}, myobject.viewModel.Messages);
Then i am basically calling this every three seconds to update html table, and it works just fine, new rows are added if new data found from server. Now i would like to add custom callback when something has actually changed, for example when new messages are found.
How should i implement this?
thanks in adv,
-hk
You could convert the two objects into json, then compare them json strings.
var messagesFromServer = getJSONData().messages; //this get msgs from server
var newString = ko.toJSON(messagesFromServer);
var oldString = ko.toJSON(myobject.viewModel.Messages);
if(newString != oldString ) {
// something new
}
ko.mapping.fromJS(messagesFromServer, {}, myobject.viewModel.Messages);
See ko.toJSON doc
I hope it helps.
If the messages is array, you can use ko.utils.compareArrays to detect the changes and raise custom events yourself. Here is code example for comparing ko.observableArray(). Look for Comparing two arrays

Value returned by getcol function in jqgrid

I have a grid using jqgrid. I need to store the some data returned by the url:, in a local variable so that I can display it in the subgrid. For this I add that column as a hidden column in the main grid, which is of the format in json:
"Details":[{"Name":"ttt", "Quantity":"12"}] .
Then in the loadcomplete: function I save the value to a variable using
var griddata = $("#grid').getCol('Details', false);
Now when I access griddata[0], I get a object Object. I tried to parse it to get the values correctly, but to no avail. I tried:
griddata[0].Details.Name
or
griddata[0].Details.0.Name
or
griddata[0].Name,
but all failed. I guess I am missing the format if the data returned by the getcol() function.I looked up the documentation on the method and it says that it returns just the values when we specify false, but could not get any examples.
If there are any examples or if there are any pointer to the solution, it will be greatly appreciated.
If you check the type of griddata[0] (for example with typeof operator) you will see that it has type of string - this is because the value you are getting is the result of toString() on the object you have passed. The reason for that is the way in which jqGrid is treating values.
If you want to store JSON as the value, you need to stringify it entirely:
"Details": "[{\"Name\":\"ttt\", \"Quantity\":\"12\"}]"
Now you can deserialize those string values into objects like this:
var griddata = $.map($("#grid").jqGrid('getCol', 'Details', false), function(value) {
return JSON.parse(value);
});
After that you can access the objects in the way you want:
var name = griddata[0].Name;
var quantity = griddata[0].Quantity;
As an alternative you can split your object into two columns, so the values can be accessed directly with getCol.

How to clear Lookup field in MS Dynamics CRM 4.0 using JavaScript

I'm trying clear the value of a lookup field via Javascript. I've tried this:
crmForm.all.new_mylookupfield.DataValue = null;
But that isn't working. I inspected the DataValue of the lookup when it was in fact cleared and it returned a null.
alert(document.getElementById("new_mylookupfield").DataValue == null); // true
I'm must be missing something here....
Thanks for the help!
UPDATE:
I finally got around to testing some of the suggestions. I'm not sure what I was doing wrong initially, but both of these methods work to clear a lookup via JavaScript:
crmForm.all.new_mylookupfield.DataValue = null;
crmForm.all.new_mylookupfield.DataValue = [];
Lookup controls have a specific type of object for their DataValue. It's an array of objects that look like this:
{
id: /* item id */,
typename: /* entity type name */,
name: /* text to display in link */
}
If you want to remove all values from the lookup, you can set it to null, but it's better to just set it to an empty array.
If you assign the value, but it doesn't seem to change anything, then you are probably not typing the correct id for the attribute. For example: If I have an entity with a lookup attribute of sneakers_brokerid, then I need to assign that value like so:
crmForm.all.sneakers_brokerid.DataValue = [];
I don't remember having to do this, but have you tried setting the value to just a new Array() with length zero?

Categories

Resources