2015-12-21 173 views
3

嗨我想刪除陣列內的數組內的元素。 我的模式是:刪除陣列內數組元素

var mySchema = Schema({ 
    primaryField:String, 
    list1: [{ 
    item1: String, 
    item2:String 
    }], 
    list2 : [{ 
    item3:String, 
    item4 : [String]//item4 is string values available in item1 
    }] 
}); 

爲前:

{ 
    "primaryField": "abc", 
    "list1": [{ 
    "item1": "aa", 
    "item2": "mystring" 
    },{ 
    "item1": "bb", 
    "item2": "sampStr" 
    }], 
    "list2": [{ 
    "item3": "stringitem", 
    "item4": ["aa", "bb"] 
    },{ 
    "item3": "sample", 
    "item4": ["bb"] 
    },{ 
    "item3": "samplestring", 
    "item4": ["aa"] 
    }] 
} 

我不得不刪除list1"aa"項目和"aa"list2.item4的發生,如果只是"aa"存在於item4然後我要刪除整個對象條目。

我的輸出應該是:

{ 
    "primaryField": "abc", 
    "list1": [{ 
    "item1": "bb", 
    "item2": "sampStr" 
    }], 
    list2:[{ 
    "item3": "stringitem", 
    "item4": ["bb"] 
    },{ 
    "item3": "sample", 
    "item4": ["bb"] 
    }] 
} 

任何人都可以幫助我實現這一目標如何使用Node.js和貓鼬。架構不能修改。

回答

1

這產生了預期的輸出。它應該足夠通用,而且我不確定它是否可以與您的代碼一起工作,但是考慮到問題中指定的內容,我認爲它應該沒問題。

function removeAllItems(obj, value) { 
    var deleted; 

    if (!obj) { 
    return false; 
    } 

    if (obj === value) { 
    deleted = true; 
    } else if (Array.isArray(obj)) { 
    for (var i = 0, len = obj.length; i < len; ++i) { 
     if (removeAllItems(obj[i], value)) { 
     obj.splice(i--, 1); 

     if (!obj.length) { 
      deleted = true; 
     } 
     } 
    } 
    } else if (typeof obj === 'object') { 
    for (var keys = Object.keys(obj), key, i = 0, len = keys.length; i < len; ++i) { 
     if (removeAllItems(obj[keys[i]], value)) { 
     delete obj[keys[i]]; 

     deleted = true; 
     } 
    } 
    } 

    return deleted; 
} 

測試用:

var test = { 
    "primaryField": "abc", 
    "list1": [{ 
    "item1": "aa", 
    "item2": "mystring" 
    },{ 
    "item1": "bb", 
    "item2": "sampStr" 
    }], 
    "list2": [{ 
    "item3": "stringitem", 
    "item4": ["aa", "bb"] 
    },{ 
    "item3": "sample", 
    "item4": ["bb"] 
    },{ 
    "item3": "samplestring", 
    "item4": ["aa"] 
    }] 
}; 

var valueToRemove = 'aa'; 

removeAllItems(test, valueToRemove); 
console.log('Removing all', valueToRemove, JSON.stringify(test, null, 2)); 

給出結果

Removing all aa { 
    "primaryField": "abc", 
    "list1": [ 
    { 
     "item1": "bb", 
     "item2": "sampStr" 
    } 
    ], 
    "list2": [ 
    { 
     "item3": "stringitem", 
     "item4": [ 
     "bb" 
     ] 
    }, 
    { 
     "item3": "sample", 
     "item4": [ 
     "bb" 
     ] 
    } 
    ] 
} 

下面是考慮到循環引用的更新,並且如果任何值被移除返回true,或false否則。

function removeAllItems(obj, value) { 
    var stack = []; 
    var found = false; 

    function _next(obj) { 
    var deleted; 

    if (!obj) { 
     return false; 
    } else if (stack.indexOf(obj) >= 0) { 
     return false; // cyclical reference found 
    } 

    stack.push(obj); 

    if (obj === value) { 
     found = deleted = true; 
    } else if (Array.isArray(obj)) { 
     for (var i = 0, len = obj.length; i < len; ++i) { 
     if (_next(obj[i])) { 
      obj.splice(i--, 1); 

      if (!obj.length) { 
      deleted = true; 
      } 
     } 
     } 
    } else if (typeof obj === 'object') { 
     for (var keys = Object.keys(obj), key, i = 0, len = keys.length; i < len; ++i) { 
     if (_next(obj[keys[i]])) { 
      delete obj[keys[i]]; 

      deleted = true; 
     } 
     } 
    } 

    stack.pop(); 

    return deleted; 
    } 

    return _next(obj), found; 
} 
+0

感謝您的回覆。當我嘗試直接使用示例數據時,它工作正常。但是當我通過find()查詢[model.find({condition},function(error,result)]的輸出時,它會拋出RangeError:超出最大調用堆棧大小。如果您有任何想法這個我不確定我在數據中犯的錯誤 – Nizam

+0

如果沒有產生這個錯誤的樣本數據很難說,如果你能提供一個錯誤的集合,那麼我會知道爲什麼你會得到這個錯誤,我可以嘗試查看我的建議......但乍一看,它應該與您指定的內容相符 –

+0

我可以看到的唯一情況是,如果您在數據中有循環引用... –