简体   繁体   English

Alasql-查询json中的嵌套对象

[英]Alasql - querying nested objects in json

I have a json object like below. 我有一个像下面的json对象。

Ebay Object 易趣对象

    {
    __v: 0
    _id: "56e192f0aea7131c15513328"
    headquarters: "New York"
    name: "Ebay"
    productCategories: [{
        _id: "56e193beaea7131c1551332d"
        name: "Footwear"
        products: [{
            name: 'Shiela',
            price: 420,
            totalSales: [10, 20]
        }, {
            name: 'Parry',
            price: 350,
            totalSales: [50, 20]
        }]
        totalSales: 100
    }, {
        1: Object
        _id: "56e193beaea7131c1551332e"
        name: "Clothes"
        products: [{
            name: 'Kurta',
            price: 210,
            totalSales: [60, 80]
        }, {
            name: 'Sun Glass',
            price: 785,
            totalSales: [5, 25]
        }],
        totalSales: 170
    }]
}

Amazon Object 亚马逊对象

{
        __v: 0
        _id: "56e192f0aea7131c15513328"
        headquarters: "New York"
        name: "Amazon"
        productCategories: [{
            _id: "56e193beaea7131c1551332d"
            name: "Footwear"
            products: [{
                name: 'Shiela',
                price: 280,
                totalSales: [10, 20]
            }, {
                name: 'Parry',
                price: 785,
                totalSales: [50, 20]
            }]
            totalSales: 100
        }, {
            1: Object
            _id: "56e193beaea7131c1551332e"
            name: "Clothes"
            products: [{
                name: 'Kurta',
                price: 150,
                totalSales: [60, 80]
            }, {
                name: 'Sun Glass',
                price: 485,
                totalSales: [5, 25]
            }],
            totalSales: 170
        }]
    }

I want to select each name inside product categories, that are common to both the companies. 我想在两个公司共同使用的产品类别中选择每个名称。

Then, I want to select the products that are common to the common product categories. 然后,我要选择常见产品类别所共有的产品。

Then I want to get the price of the common products (to both the companies) for comparison 然后,我想获取普通产品(两家公司)的价格进行比较

I am able to run the below query 我可以运行以下查询

 alasql('SELECT products FROM ? AS CATEGORY1 JOIN ? AS CATEGORY2 USING [0]', [$scope.company1.productCategories, $scope.company2.productCategories], function(data) {
                    console.log("join query executed");
                    console.log(data);
                });

I want to find the products inside each product category. 我想在每个产品类别中找到产品。 I want a query like 我想要一个查询

alasql('SELECT products.name,products.price FROM ? as category1 join ? as category2 using products.name', [$scope.company1.productcategories,$scope.company2.productcategories], function(data) {
                    console.log("Query executed");
                    console.log(data);
                });

But this errors out. 但是这个错误出来了。

Please let me know the correct procedure. 请让我知道正确的程序。

Regards, Sabarisri 问候,Sabarisri

First, when you are only working on local memory there is on reason to make it async. 首先,当您仅在本地内存上工作时,就有理由使其异步。 So your first change should be to use 所以你的第一个改变应该是使用

var res = alasql('SELECT productCategories->0->name FROM ?',[$scope.selectedCompanies]);

You are very close. 你很亲密 You basically want to query on only the productCategories data - so you should do 您基本上只想查询productCategories数据-因此您应该这样做

var res = alasql('SELECT name FROM ?',[$scope.selectedCompanies.productCategories]);

Bonus info: If you want to join with something you could do something like 奖励信息:如果您想加入某项活动,可以做类似的事情

var res = alasql('SELECT p.name, d.stock FROM ? p JOIN ? d ON p.name = d.company',[$scope.selectedCompanies.productCategories, $scope.otherData]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM