2015-10-16 60 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>