0

Can not find Rx.Observable.pairs in Rxjs5, what I need just convert an object into Observable and inspect the change for each property. any ideas?

 var a = { aa: "aa", bb: "bb" };
   function pairs(obj) {
    // List of object's key-value pairs
    var keyValuePairs = Object.keys(obj).map(key => ({ key, value: obj[key] }));
    // Convert to an Observable and return
    return Rx.Observable.from(keyValuePairs);
    }

  var xxx = pairs(a);
xxx.subscribe(x => {
    console.log(x);
})

a.aa = "mm";
VincentGuo
  • 247
  • 2
  • 9

1 Answers1

0

You can accomplish this from scratch:

function pairs(obj) {
    // List of object's key-value pairs
    var keyValuePairs = Object.keys(obj).map(key => ({ key, value: obj[key]}));

    // Convert to an Observable and return
    return Rx.Observable.from(keyValuePairs);
}
Calvin Belden
  • 3,114
  • 1
  • 19
  • 21
  • I add the sample code, please help me on why when the properties change, I didn't get any log:Object {key: "aa", value: "aa"} Object {key: "bb", value: "bb"} – VincentGuo Mar 23 '16 at 09:13
  • The code that I provided will not emit an event when properties change. Rather, it looks at the items *current* properties and creates an Observable based on those: this is how `Rx.Observable.pairs` in v4 worked. – Calvin Belden Mar 23 '16 at 14:56
  • you are right. pairs maybe not what I want. I want to have an Observable to observer any changes on an Object. could you please give me any instructions? – VincentGuo Mar 24 '16 at 06:09
  • Any luck with this ? ive been looking for this functionality too – Kravitz Sep 10 '17 at 14:31