1

edit 1: my question was marked as duplicated but it is not answering my question, i don't want to merge but intersect bar in foo.. If bar holds extra properties, merging bar in foo will result in having foo with 3 properties. what i want is to copy the values of the properties of bar that are also presents in foo object.

I hold two objects

var foo = { id:8, name:"luke" }
var bar = { id:9, name:"vador" }

the view keeps a reference on foo object, then foo=bar will not make the view updated as foo has a new reference to data.

Then my question is how can I intersect bar in foo as to keep foo's reference ?

something similar to

foo.id = bar.id
foo.name = bar.name

but as foo and bar Object may have a lot of properties this would become tedious to write all manually.

What is the most proper way to intersect without any use of js frameworks ?

vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • 1
    the duplicate mark doesn't help, please remove, i don't want to merge, but intersect – vdegenne May 20 '15 at 12:26
  • Don't you want to merge `bar` into `foo`, i.e. copy the properties of `bar` to `foo`, including those who already exist in `foo`? If that's the case, then the duplicate is appropriate. If it's not, then please elaborate on what you actually want to achieve. – Frédéric Hamidi May 20 '15 at 12:28
  • Also, three upvotes after the question was closed as a duplicate? You must have a lot of fans :) – Frédéric Hamidi May 20 '15 at 12:29
  • @FrédéricHamidi I'm guessing he's using angularJs as he talks about "view updating". So he doesn't want the var `foo` to change reference, but to rather update. The answer in your duplicate is changing the reference. – Jordumus May 20 '15 at 12:29
  • @Jordumus, I'm afraid not. The first code snippet in the accepted answer would not change the reference to `foo` whatsoever. – Frédéric Hamidi May 20 '15 at 12:30
  • 1
    @FrédéricHamidi I didn't see that first line in that answer. You're right, it would solve the question asked. - Edit to Op: the duplicate flag was appropriate. – Jordumus May 20 '15 at 12:32
  • 1
    @FrédéricHamidi Saying that I'm a fan of a user I've never heard before is weird. I voted because the given duplicate is not what this question answers. – A1rPun May 20 '15 at 12:34
  • @A1rPun actually, it does, check the very first line of the first answer. It does "overwrite" the properties from array1. – Jordumus May 20 '15 at 12:35
  • @A1rPun, leaving aside the fact that votes are meant to reflect the quality of the question, not to be used because "the duplicate is inappropriate", it looks like it was appropriate after all. – Frédéric Hamidi May 20 '15 at 12:35
  • *Sigh...* or not. The last edit by the questioner specifies more clearly that properties of `bar` that do not exist in `foo` should not be copied. Reverting duplicate... – Frédéric Hamidi May 20 '15 at 12:38
  • @FrédéricHamidi You are right about the meaning of votes.. except that votes equals fans ;) – A1rPun May 20 '15 at 12:39
  • so you want foo = bar? – Mathijs Segers May 20 '15 at 12:41
  • 1
    @A1rPun, yup, that was a kinda pun (meta-pun not intended) because sometimes this voting pattern is due to "friends or coworkers" upvoting a user *en masse*. Not that it's the case here, of course. – Frédéric Hamidi May 20 '15 at 12:41
  • 1
    @FrédéricHamidi I can't believe you are right, but I got to admit you are. but the fans thing was so bold. If I really want followers, I'd make a twitter account. – vdegenne May 20 '15 at 12:42

1 Answers1

2
var foo = { id:8, name:"luke" };
var bar = { id:9, name:"vador" };

for (var key in bar) {
    if (key in foo)
        foo[key] = bar[key];
}

console.log (foo);

if (key in foo) checks if it has the property, if it doesnt, nothing happens.

Thalsan
  • 5,140
  • 1
  • 11
  • 12