使用remove从数组中删除对象

时间:2015-02-08 23:27:08

标签: javascript parse-platform

假设:

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

我需要更多关于如何表达关键和价值的细节。我查看了文档,我无法理解。我花了好几天时间。幽默我,拜托,我是新手,我很痛苦!

当我列出的时候(“对象”),这是我的终端在我的终端后显示的内容。 我想通过_id删除对象。在最底部你会看到'false'我在哪里做LIST.REMOVE(“_ id”,[_ids of _ids]):

[ { _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

2 个答案:

答案 0 :(得分:1)

要删除的操作数需要等于要删除的对象。首先找到你要删除的对象......

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在这种阵列管理方面也很出色。

答案 1 :(得分:0)

您可以尝试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;
});