Watch.JS icon indicating copy to clipboard operation
Watch.JS copied to clipboard

question about using arrays of objects

Open edwardsmarkf opened this issue 6 years ago • 2 comments

hello -

if i were using an array of objects like this:

var obj = [
{
    id: 1,
    firstName: 'Mark Jones', 
    address: '123 Swallow Lane',
},
{
    id: 2,
    firstName: 'Lori Smith', 
    address: '948 South Elm Street',
}
];


WatchJS.watch(obj, (prop, action, newvalue, oldvalue) => {

is it possible to get the row number? this works great: obj[0].firstName='MARK'; // triggers alert with property, oldvalue and newvalue Is there any way to determine the changed row number, or perhaps the id value?

also, is there any way to watch for inserts and deletes?

thank you very much.

edwardsmarkf avatar Apr 03 '18 17:04 edwardsmarkf

var list=[
    {value:"test 1"},
    {value:"test 2"},
    {value:"test 3"}
]

watch(list,function(name,type,val,oldval){
    // get index
    var index = list.indexOf(this);

    console.log(index,name,type,val,oldval);
});

list[1].value="new value";
// 1 'value' 'set' 'new value' 'test 2'

bonarja avatar May 09 '18 16:05 bonarja

I need to be able to do this too. Writing var index = list.indexOf(this); always returns a value of -1 for index and { get: [Function] } for this. Am I missing something? Can can I tell the index of the object that was changed in when watching an array of objects?

slutske22 avatar Jan 07 '21 17:01 slutske22