简体   繁体   English

合并Java共享密钥上的多个数组

[英]Merging multiple arrays on shared keys in Javascript

I want to merge multiple arrays into one big array with shared keys. 我想使用共享密钥将多个数组合并为一个大数组。

What I've tried: 我尝试过的

var conditions = [];
if( aa != undefined )
{
    conditions.push( { "query" : { "must" : {  "aa" : "this is aa" } } } );
}

if( bb != undefined )
{
    conditions.push( { "query" : { "must" : {  "bb" : "this is bb" } } } );
}

The above code is giving: 上面的代码给出:

[
    {
        "query": {
            "must": {
                "aa": "this is aa"
            }
        }
    },
    {
        "query": {
            "must": {
                "bb": "this is bb"
            }
        }
    }
]

But I need this: 但是我需要这个:

[
    {
        "query": {
            "must": [
                {
                    "aa": "this is aa"
                },
                {
                    "bb": "this is bb"
                }
            ]
        }
    }
]

I am able to do it with PHP but I need to do it in native javascript or using underscore.js 我可以用PHP做到这一点,但我需要使用本机javascript或使用underscore.js来做到这一点。

Define the object your pushing - push everything to the inner array first - then push the object to the outer array: 定义要推送的对象-首先将所有内容推送到内部数组-然后将对象推送到外部数组:

var conditions = [];
var query = { query: {} };
if( aa != undefined ) {
    if (!query["query"]["must"]) {
        query["query"]["must"] = [];
    }
    //conditions.push( { "query" : { "must" : {  "aa" : "this is aa" } } } );
    query["query"]["must"].push({  "aa" : "this is aa" });
}

if( bb != undefined ) {
    if (!query["query"]["must"]) {
        query["query"]["must"] = [];
    }
    //conditions.push( { "query" : { "must" : {  "bb" : "this is bb" } } } );
    query["query"]["must"].push({  "bb" : "this is bb" });
}

conditions.push(query);

It's not quite trivial, because I see you want to make an array out of the last inner property. 这不是很简单,因为我看到您想用最后一个内部属性制成一个数组。

Do those Objects you push into the conditions array already exist or are you defining them yourself? 您放入条件数组的那些对象是否已经存在,或者您是自己定义的?

You can solve your problem with a recursive function like this I believe: 我相信您可以使用这样的递归函数解决问题:

EDIT: The code produces the exact result you wanted now. 编辑:该代码产生您现在想要的确切结果。

var object1 = {
    query: {
        must: {
            aa: "this is aa"
        }
    }
}

var object2 = {
    query: {
        must: {
            bb: "this is bb"
        }
    }
}

var conditions = {};

function mergeObjects(object, parentObject){
    for(var prop in object){
        if(parentObject.hasOwnProperty(prop)){
            if(typeof parentObject[prop] === "object" && shareProperties(parentObject[prop], object[prop])){
                mergeObjects(object[prop], parentObject[prop])
            }else{
                parentObject[prop] = [parentObject[prop], object[prop]];
            }
        }else{
            parentObject[prop] = object[prop];
        }   
    }
}

function shareProperties(obj1, obj2){
    for(var prop in obj1){
        if(obj2.hasOwnProperty(prop)){
            return true;
        }
    }
    return false;
}

mergeObjects(object1, conditions);
mergeObjects(object2, conditions);

Output: 输出:

"{"query":{"must":[{"aa":"this is aa"},{"bb":"this is bb"}]}}"

For each descendant of conditions , check if it exists, create it if it doesn't. 对于conditions每个后代,检查它是否存在,如果不存在,则创建它。

Then, finally, push your new object: 然后,最后,推送您的新对象:

 function addCondition(conditions, key, value) { conditions[0] = conditions[0] || {}; conditions[0].query = conditions[0].query || {}; conditions[0].query.must = conditions[0].query.must || []; var o = {}; o[key] = value; conditions[0].query.must.push( o ); } var conditions = []; var aa = 1, bb = 1; if (typeof(aa) !== 'undefined') addCondition(conditions, "aa", "this is aa" ); if (typeof(bb) !== 'undefined') addCondition(conditions, "bb", "this is bb" ); if (typeof(cc) !== 'undefined') addCondition(conditions, "cc", "this is cc" ); document.getElementById('results').innerHTML = JSON.stringify(conditions, null, 2); 
 <pre id="results"></pre> 

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

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