Combine 2 JSON objects in JQUERY - ajax

Could you please help me on how to combine the below Result1 and Result2 JSON objects into single JSON such that i have Name,No,Avg,Subject1,Subject2 into single JSON object. I am using it in JQUERY AJAX.
{"Result1":"[{"NAME" : "Mark","No" : "23544","Avg" : "49"}]"}
{"Result2":"[{"Subject1" : "Maths","Subject2" : "Computers"}]"}
Please help.
Thanks

See jQuery.extend()
var x = {"Result1":"[{"NAME" : "Mark","No" : "23544","Avg" : "49"}]"}
var y = {"Result2":"[{"Subject1" : "Maths","Subject2" : "Computers"}]"}
var z = jQuery.extend({}, x.Result1[0], y.Result2[0]);
// z.NAME, z.No, z.Avg, z.subject1...
I'm not sure whether you've parsed the JSON string into a JavaScript object yet; but see jQuery.parseJSON() to see how you do this (be aware; parseJSON() will throw an error if you pass it invalid JSON).

Related

Problem in adding multi filter to odata service

I have the following code. I am trying to read 3 different values for PO from input fields then displaying the result in list. Program is working fine for single input but for multiple inputs i am facing issues.
var oV1 = this.getView().byId("oInput").getValue();
var oV2 = this.getView().byId("oInput1").getValue();
var oV3 = this.getView().byId("oInput2").getValue();
var oFilter = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV1)];
var oFilter1 = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV2)];
var oFilter2 = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV3)];
var orFilter =new Array(new sap.ui.model.Filter({filters:[oFilter, oFilter1, oFilter2],and:true}));
var oView1 = this.getView();
var oTable = oView1.byId("myTable");
var oBinding = oTable.getBinding("items");
if(oV1 === "")
{
oBinding.filter( [] );
oBinding.refresh(true);
}
else
{
oBinding.filter(orFilter);
At the above oBinding.filter I am receiving following error.
Filter in Aggregation of Multi filter has to be instance of sap.ui.model.Filter -
Unable to get property 'replace' of undefined or null reference
Please help.
You wrapped your filters in 1000 layers of Arrays which doesn't make sense.
Just create a single array which contains Filter objects:
// Filter, FilterOperator, and FilterType required from "sap/ui/model/*"
const aFilter = [
new Filter("Ebeln", FilterOperator.Contains, sV1),
new Filter("Ebeln", FilterOperator.Contains, sV2),
new Filter("Ebeln", FilterOperator.Contains, sV3),
];
oListBinding.filter(aFilter, FilterType.Application);
I know this isn't a code review but some suggestions:
No one uses new Array(). Just use [].
Also you used Hungarian notation but every one of your variable names begins with o. o means object. Some of your variables are not objects but strings (like oV1, better would be sV1) or arrays.
For the filters aggregation, sap.ui.model.Filter accepts a Filter[]. But because oFilter already is a Filter[], [oFilter, oFilter1, oFilter2] is a Filter[][], so that won't work.
To make it work, just remove the surrounding [] from the filter definitions, like so:
var oFilter = new Filter(...);.

How to convert ReversedRandomAccessCollection to regular collection

I've been trying to find a way to sort my resulting MPMediaQuery so that the results are sorted by date.
I've had a lot of difficulty, and then I found the "reversed()" method, but I cannot get the returned data into a format that I can use with the MPMediaPlayer.
My original query:
var qryPodcasts = MPMediaQuery()
var titleFilter = MPMediaPropertyPredicate()
titleFilter = MPMediaPropertyPredicate(value: "This American Life", forProperty: MPMediaItemPropertyPodcastTitle, comparisonType: .equalTo)
qryPodcasts.addFilterPredicate(titleFilter)
So I tried this:
let myItems = qryPodcasts.items?.reversed()
podCollection = MPMediaItemCollection(items: myItems!) //ERROR HERE
myMP.setQueue(with: podCollection!)
Which gave me this error:
"Cannot convert value of type 'ReversedRandomAccessCollection<[MPMediaItem]>' (aka 'ReversedRandomAccessCollection<Array<MPMediaItem>>') to expected argument type '[MPMediaItem]'"
How can I use the results of 'reversed()' in the MediaPlayer?
what about this?
let myItems = qryPodcasts.items?.reversed()
podCollection = MPMediaItemCollection(items: Array(myItems)!)
myMP.setQueue(with: podCollection!)

String' is not convertible to 'StringLiteralConvertible' in Xcode 7 Beta 5

I'm trying to initialize a dictionary constant with
["updateType" : "moveRow", "data" : rows].
rows is an array. I have is as:
let update = ["updateType" : "moveRow", "data" : rows]
I have also tried:
let update: Dictionary< String, AnyObject> = ["updateType" : MoveRow", "data" : rows]
and
let update: [String : AnyObject] = ["updateType" : "moveRow", "data" : rows]
in each case, I get and error on the key "data" that says:
String' is not convertible to 'StringLiteralConvertible'.
Can anybody explain what's going on?
The problem seems to be in your creation of rows as optional:
var rows: Array<Dictionary<String, AnyObject>>?
If you can get rid of the optional, it should start working. E.g.
let d = ["updateType" : "moveRow", "data" : rows!]
...or by creating the rows like this:
var rows = Array<Dictionary<String, AnyObject>>()

Unexpected behaviour in linq query with lazy loading on ravendb

I had a problem with the following code. When I don't call ToList() on the initial RavenSession.Query<Item>() call, the PhotoPath property is null in the ItemSummaryModel object. Is this a lazy loading issue or something else that's causing this?
The PhotoPath property was null on the initial save of this document. I then updated it in a subsequent edit.
When I query for the full item instead of selecting a new object it works as expected populating all properties.
Why did I have to force query execution with ToList() for the new ItemSummaryModel to be populated as expected?
var fullItems = RavenSession.Query<Item>().ToList();
var items = (from i in fullItems
where i.DateAdded >= DateTime.Now.Subtract(new TimeSpan(10,0,0,0))
orderby i.DateAdded
select new ItemSummaryModel()
{
Id = i.Id,
PhotoPath = i.ListingPhotoPath,
MarketingInfo = i.MarketingInfoShort,
Name = i.Name,
Summary = i.Summary,
PriceTypeCode = i.ClearancePrice > 0 ? PriceType.Clearance : (i.SalePrice > 0 ? PriceType.Sale : PriceType.List),
ListSaleOrClearancePrice = i.ClearancePrice > 0 ? i.ClearancePrice : (i.SalePrice > 0 ? i.SalePrice : i.Price)
}).Take(nbrOfItems);
return items;
RavenDB's linq provider is pretty simplistic, it can't currently handle field remapping.
In other words, it can't handle it that you did this:
PhotoPath = i.ListingPhotoPath,
If you changed it to
ListingPhotoPath = i.ListingPhotoPath,
It will work.
That is an issue that is scheduled to be fixed

Invoke an Expression in a Select statement - LINQ to Entity Framework

I'm trying to use an already existing Expression building class that I made when trying to do a select clause, but I'm not sure how to attach the expression to the expression tree for the Select, I tried doing the following:
var catalogs = matchingCatalogs.Select(c => new
{
c.CatalogID,
Name = EntitiesExpressionHelper.MakeTranslationExpression<Catalog>("Name", ApplicationContext.Instance.CurrentLanguageID).Compile().Invoke(c),
CategoryName = EntitiesExpressionHelper.MakeTranslationExpression<Category>("Name", ApplicationContext.Instance.CurrentLanguageID).Compile().Invoke(c.Category),
c.CategoryID,
c.StartDateUTC,
c.EndDateUTC
});
But I obviously get the error stating that the Entity Framework can't map Invoke to a SQL method. Is there a way to work around this?
FYI, EntitiesExpressionHelper.MakeTranslationExpression<T>(string name, int languageID) is equivalent to:
x => x.Translations.Count(t => t.LanguageID == languageID) == 0 ? x.Translations.Count() > 0 ? x.Translations.FirstOrDefault().Name : "" : x.Translations.FirstOrDefault(t => t.LanguageID == languageID).Name
EDIT: I realize that I need to use an ExpressionVisitor to accomplish this, but I'm not sure how to use an ExpressionVisitor to alter the MemberInitExpression, so if anyone knows how to accomplish this, let me know.
You need to capture the expressions in vars. You won't be able to use anonymous types. The general idea is that this works:
Expression<Func<Foo, Bar>> exp = GenExpression();
var q = matchingCatalogs.Select(exp);
But this will not:
var q = matchingCatalogs.Select(GenExpression());
The first happily passes the result of GenExpression to L2E. The second tries to pass GenExpression itself to L2E, rather than the result.
So you need a reference to a var of the same type as the expression. Those can't be implicitly typed, so you'll need a real type for your result type.

Resources