簡體   English   中英

使用remove從數組中刪除對象

[英]Deleting an object from an array using remove

鑒於:

var object = {key: value, key1: value, key2: value}
var array = [{object}, {object1}, {object2}, {object3}]

我想使用解析javascript SDK從數組中刪除對象3和4。 使用其key2值。 我該怎么做呢?

我相信它會像這樣:

object.remove("the key", [object2value2, object3value2])

我需要有關如何闡明鍵和值的更多詳細信息。 我看了看文檔,卻發現不了。 我已經花了好幾天了。 幽默我,請我是新手,我正在受苦!

當我列出.GET(“ OBJECT”)時,在我的解析查詢后在終端顯示的內容是什么? 我想通過_id刪除對象。 在最底端,您在執行LIST.REMOVE(“ _ id”,[_ids數組])時看到“ false”:

[ { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
    _id: 'QllVljV252iNZej9VQgBCYkEyD4Do9fvZMAvmK',
    amount: 2307.15,
    category: [ 'Shops', 'Computers and Electronics' ],
    category_id: '19013000',
    date: '2014-06-23',
    meta: { location: [Object] },
    name: 'Apple Store',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'NQQVQJVDgDhj90JvnXkMt1jm06eqzji5JvO52Z',
    amount: 3.19,
    category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
    category_id: '13005043',
    date: '2014-06-21',
    meta: { location: [Object] },
    name: 'Gregorys Coffee',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'Jwwrw1rnjnfXPvmG9KlZtDoXbQnW1VIMvwrMKp',
    amount: 80,
    category: [ 'Transfer', 'Withdrawal', 'ATM' ],
    category_id: '21012002',
    date: '2014-06-08',
    meta: { location: [Object] },
    name: 'ATM Withdrawal',
    pending: false,
    score: { location: [Object], name: 1 },
    type: { primary: 'special' } },
  { _account: 'mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz',
    _id: 'aWWVW4VqGqIdaP495pmetGRqAVKrLRFMD5bMrX',
    amount: -240,
    category: [ 'Transfer', 'Account Transfer' ],
    category_id: '21001000',
    date: '2014-06-02',
    meta: { location: {} },
    name: 'Online Transfer from Chk ...1702',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'ZnnVnDVbybCqG4DV1BMgCPyAgyDz9vSA2Y5AG1',
    amount: 240,
    category: [ 'Transfer', 'Account Transfer' ],
    category_id: '21001000',
    date: '2014-06-01',
    meta: { location: {} },
    name: 'Online Transfer to Sav ...9606',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'WOOVOlVrqrHaVDlAdGPmUAKg5k4qBafkZjRkb2',
    amount: -0.93,
    category: [ 'Interest' ],
    category_id: '15000000',
    date: '2014-05-17',
    meta: { location: {} },
    name: 'Interest Payment',
    pending: false,
    score: { location: {}, name: 0.2 },
    type: { primary: 'unresolved' } },
  { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
    _id: '600r0LrVvViXjq96lBpdtyOWboBvzmsaZoeaVz',
    amount: 12.74,
    date: '2014-05-12',
    meta: { location: [Object] },
    name: 'Golden Crepes',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'pQQJQ9J0k0hqAVbDwMmYCrajm2JE6OUNBvwNYa',
    amount: 7.23,
    category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
    category_id: '13005043',
    date: '2014-05-09',
    meta: { location: [Object] },
    name: 'Krankies Coffee',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
    _id: '2DD4Dl4nJnCPn4YRJK95hvwgWda5y2SWdDkW6m',
    amount: 118.23,
    category: [ 'Shops', 'Digital Purchase' ],
    category_id: '19019000',
    date: '2014-04-26',
    meta: { location: {} },
    name: 'Banana Republic',
    pending: false,
    score: { location: {}, name: 0.2 },
    type: { primary: 'digital' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'oGGNG1NwYwUZQGOB5yjlhYMKG6yMQGtaON9aLd',
    amount: -800,
    category: [ 'Transfer', 'Third Party', 'Venmo' ],
    category_id: '21010001',
    date: '2014-04-20',
    meta: { location: {} },
    name: 'Venmo Cashout 18375552',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } },
  { _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
    _id: 'pQQJQ9J0k0hqAVbDwMmYCrapBJba4BSNBvwNYk',
    amount: 120,
    category: [ 'Transfer', 'Third Party', 'Venmo' ],
    category_id: '21010001',
    date: '2014-04-19',
    meta: { location: {} },
    name: 'Venmo Payment 16991172',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } },
  { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
    _id: '055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2',
    amount: 5.32,
    category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
    category_id: '13005043',
    date: '2014-04-17',
    meta: { location: [Object] },
    name: 'Octane Coffee Bar and Lounge',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
    _id: 'LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp',
    amount: 28.57,
    category: [ 'Food and Drink', 'Restaurants', 'Pizza' ],
    category_id: '13005012',
    date: '2014-04-11',
    meta: { location: [Object] },
    name: 'Papa Johns Pizza',
    pending: false,
    score: { location: [Object], name: 0.2 },
    type: { primary: 'place' } },
  { _account: 'mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz',
    _id: 'rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61',
    amount: -3042.44,
    category: [ 'Transfer', 'Payroll' ],
    category_id: '21009000',
    date: '2014-03-27',
    meta: { location: {} },
    name: 'Company Payroll',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } },
  { _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
    _id: '944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB',
    amount: 200,
    category: [ 'Transfer', 'Withdrawal', 'ATM' ],
    category_id: '21012002',
    date: '2014-07-21',
    meta: { location: [Object] },
    name: 'ATM Withdrawal',
    pending: false,
    score: { location: [Object], name: 1 },
    type: { primary: 'special' } },
  { _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
    _id: 'rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK',
    amount: 240,
    category: [ 'Transfer', 'Account Transfer' ],
    category_id: '21001000',
    date: '2014-07-24',
    meta: { location: {} },
    name: 'Online Transfer from External Sav ...3092',
    pending: false,
    score: { location: {}, name: 1 },
    type: { primary: 'special' } } ]
false

刪除操作數需要等於要刪除的對象。 因此,首先找到要刪除的對象...

var array = myObject.get("theArrayCol");
var removeMe;
for (var i=0; i < array.length; i++) {
    if (array[i].key2 == "this one should be removed")
        removeMe = array[i];
}

然后將其刪除...

myObject.remove("theArrayCol", removeMe);

編輯-根據我們的聊天,這是在您的情況下應用此方法的方法。 我將代碼分解為更簡單的函數,每個函數都可以輕松定義操作。 我希望它使它更易於理解,並且我認為它仍然是良好的編程習慣...

// token is your key to search the Transaction table in parse
function transactionWithToken(token) {
    var query = new Parse.Query("Transactions");
    query.equalTo("access_token", token); 
    query.select("transactions");
    return query.first();
}

// array is the value of the array column on the Transaction table
// transactionId is a string that might match the value of the _id property in the array of objects
function transactionInArrayWithId(array, transactionId) {
    for (var i=0; i<array.length; i++) {
        if (array[i]._id == transactionId) return array[i];
    }
    return undefined;
}

function removeTransactionWithId(transaction, transactionId) {
    var array = transaction.get("transactions");
    var t = transactionInArrayWithId(array, transactionId);
    transaction.remove("transactions", t);
}

// token is the key to the Transaction table
// transactionIds is an array of ids to remove from the Transaction object's transactions array
function removeTransactionsWithIdsFromToken(token, transactionIds) {
    return transactionWithToken(token).then(function(result) {
        for (var i=0; i<transactionIds.length; i++) {
            removeTransactionWithId(result, transactionIds[i]);
        }
        return result.save();
    });
}

如果列名和表名不太相似,這將更容易理解。 另外,underscorejs非常適合這種陣列管理。

您可以嘗試對其進行filter 例如,如果您要刪除鍵“ k3”的值為3的所有對象;

var obj1 = {k1: 1, k2: 2, k3: 3};
var obj2 = {k1: 4, k2: 5, k3: 6};
var obj3 = {k1: 7, k2: 8, k3: 9};
var array = [obj1, obj2, obj3];

var badValue = 3;
var result = array.filter(function(obj){
    return obj.k3 !== badValue;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM