弹性搜索使用javascript将json对象的数组展平为单个级别

时间:2018-05-14 00:07:01

标签: arrays json object flatten

我面临的问题是将我的弹性查询响应(只有我想要_source详细信息)展平为单级数组。

请在下面找到弹性响应:

使用(response.hits.hits)我能够访问数据数组但无法将对象数组展平为单个级别

当前数组:

var current_array = [
    {
        "_id": "gSdhs8aPjcUjiIs6ey85",
        "_score": 1,
        "_source": {
            "business_type": "Primary Only",
            "ceo_name": "Tim Cook",
            "city": "San Ramon",
            "company_id": "gSdhs8aPjcUjiIs6ey85",
            "company_name": "Apple Inc."
        }
    },
    {
        "_id": "v2bdB5F3PbLzziayKikS",
        "_score": 1,
        "_source": {
            "bankruptcy_activity": "No",
            "business_type": "Primary Only",
            "ceo_name": "Andrew N. Liveris",
            "city": "New York",
            "company_id": "v2bdB5F3PbLzziayKikS",
            "company_name": "General Electric",
            "country": "United States"
        }
    }
];

期望的结果:

var desired_array = [{
        "_id": "gSdhs8aPjcUjiIs6ey85",
        "_score": 1,
        "_source_business_type": "Primary Only",
        "_source_ceo_name": "Tim Cook",
        "_source_city": "San Ramon",
        "_source_company_id": "gSdhs8aPjcUjiIs6ey85",
        "_source_company_name": "Apple Inc."
    },
    {
        "_id": "v2bdB5F3PbLzziayKikS",
        "_score": 1,
        "_source_business_type": "Primary Only",
        "_source_ceo_name": "Andrew N. Liveris",
        "_source_city": "New York",
        "_source_company_id": "v2bdB5F3PbLzziayKikS",
        "_source_company_name": "General Electric",
        "_source_country": "United States"
    }
];

我尝试了以下代码,但它正在转换为我不想要的单个数组:

var flattenObject = function (ob) {
    var toReturn = [];
    var flatObject;
    for (var i in ob) {
        if (!ob.hasOwnProperty(i)) {
            continue;
        }
        if ((typeof ob[i]) === 'object') {
            flatObject = flattenObject(ob[i]);
            for (var x in flatObject) {
                if (!flatObject.hasOwnProperty(x)) {
                    continue;
                }
                toReturn[i + (!!isNaN(x) ? '.' + x : '')] = flatObject[x];
            }
        } else {
            toReturn[i] = ob[i];
        }
    }
    return toReturn;
};

它输出如下:

var undesired_array = [
    0. _index: "company",
    0. _score: 1,
    0. _source.ceo_name: "Tim Cook",
    0. _source.city: "San Ramon",
    0. _source.company_name: "Apple Inc.",
    1. _id: "v2bdB5F3PbLzziayKikS",
    1. _index: "company",
    1. _score: 1,
    1. _source.ceo_name: "Andrew N. Liveris",
    1. _source.city: "New York",
    1. _source.company_id: "v2bdB5F3PbLzziayKikS",
    1. _source.company_name: "General Electric"
];

1 个答案:

答案 0 :(得分:0)

考虑到每个对象中只有一层嵌套,此代码应该起作用。

function flattenObject(input) {
    return input.map(function (doc) {
        let flatDoc = {};
        Object.keys(doc).forEach(function (key) {
            if (doc[key]instanceof Object) {
                Object.keys(doc[key]).forEach(function (innerkey) {
                    flatDoc[key + "_" + innerkey] = doc[key][innerkey];
                });
            } else {
                flatDoc[key] = doc[key];
            }
        });
        return flatDoc;
    });
}