2015-10-16 56 views
0

我想将多个数组合并成一个带有共享密钥的大数组。在Javascript中共享共享密钥上的多个数组合并

我已经试过什么:

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

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

上面的代码是给:

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

但我需要这样的:

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

我可以用PHP做但我需要在原生JavaScript或使用underscore.js

+0

因为我从PHP转换我的应用程序的NodeJS –

回答

1

定义你推对象 - 首先推动一切内阵列 - 然后推对象外阵列:

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); 
+0

它给错误“遗漏的类型错误:无法读取属性‘必须’的undefined“ –

+0

@MohammadShahid - 编辑。 – tymeJV

+0

这只有在他自己定义条件时才有效。它不起作用,如果他们已经存在,他只是想合并另一个对象在 – Danmoreng

1

这不是很琐碎,因为我看到你想使一个数组出最后的内在属性。

您推入条件数组的那些对象是否已经存在,或者您是否自己定义它们?

你可以用这样的递归函数,我相信您解决问题:

编辑:代码产生你现在想确切的结果。

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); 

输出:

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

我需要根据条件合并多个数组 –

+0

检查我编辑的代码,这应该解决你的问题。 – Danmoreng

1

对于conditions每个后代,检查它是否存在,创建它,如果它不。

然后,最后,把你的新对象:

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>