0

Given:

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

I want to use the parse javascript SDK to delete object 3 and 4 from the array. Using their key2 values. How do I do this?

I believe it goes something like:

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

I need more detail on how to articulate the key and the value. I looked at the docs and I just can't figure it out. I've spent days on this. Humor me, please I'm a newbie and I'm suffering!

THIS IS WHAT SHOWS IN MY TERMINAL AFTER MY PARSE QUERIES WHEN I LIST.GET("OBJECT"); I'd like to delete objects by _id. At the very bottom you see 'false' where I do LIST.REMOVE("_id", [array 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
5
  • about being a newbie, it will help you get more/better answers on SO if you get in the habit of marking correct answers when they are given. it looks like you'v done that about once in >25 answers received. Commented Feb 8, 2015 at 23:56
  • 1
    Actually I only missed one person that answered one of my questions. The rest of the answers are things I figured out and went back to give an answer for the record should anyone else have the problem. I fixed the person I missed and gave them an upvote. So maybe I didin't upvote all of the answers because I can't upvote myself . .. Commented Feb 9, 2015 at 0:05
  • I would accept answers as I have a few notes on that but I don't see where that is. Commented Feb 9, 2015 at 0:11
  • Sorry, maybe I should have looked more carefully at your history. To accept an answer, check the little checkmark that appears under the votes counter on the upper left. Commented Feb 9, 2015 at 0:12
  • thanks! I'll go back and do that now Commented Feb 9, 2015 at 0:18

2 Answers 2

1

The operand to remove needs to equal the object being removed. So first find the object you wish to remove...

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];
}

then remove it...

myObject.remove("theArrayCol", removeMe);

EDIT - based on our chat, here's how to apply this in your situation. I broke the code up into simpler functions, each doing an easily definable operation. I hope it makes it easier to understand, and I think its good programming practice anyway...

// 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();
    });
}

This would be easier to understand if the column name and the table name weren't so similar. Also, underscorejs is great at this sort of array management.

Sign up to request clarification or add additional context in comments.

15 Comments

I'm not clear what you mean "theArrayCol". I know the value of removeMe already and have an array of removeMe. right now I'm using object.remove("key2", [key2ValuesToRemove]); This is not working.
The first param to remove is the name of the column on your object that has the array type. Is that called "the key" on your object?
yes, in my example it is referred to as "key2". We are on the same page there
Ya, I keep getting 'false' for that solution in my terminal
This solution keeps returning 'false' in my terminal. Very frustrating.
|
0

you can try to filter it. For example if you want to remove all objects which key 'k3' has value of 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;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.