2

I'm not exactly sure how I should phrase it but basically I want to achieve such that object a:

var a = {
"test123": "A",
"testing/test": "B",
"notest": "C"
};

and object b:

var b = {
"test123": "Test 123",
"testing": "Testing"
};

combine such that there is an object c that looks like this:

var c = {
"Test 123": "A",
"Testing/test": "B",
"notest": "C"
};

I hope you understand what I'm talking about. Basically combine / replace objects a and b into a c such that only the text before / is replaced.

Thanks!

EDIT:

Just in case you don't get it, this is what I mean.

In object b, b['test123'] = 'Test 123' so that a['test123'] should turn into c['Test 123'] because it is changed based on b.

Also since b['testing'] = 'Testing', a['testing/test'] would turn into c['Testing/test'] as stated in b, just that the text after the / is not affected.

  • Its not clear what you are trying to do. Please explain how each of the keys in `c` got transformed – thefourtheye May 16 '15 at 11:48
  • @thefourtheye In object `b`, `b['test123'] = 'Test 123` so that `a['test123']` should turn into c['Test 123'] because it is changed based on `b`. Also since `b['testing'] = 'Testing'`, `a['testing/test']` would turn into c['Testing/test'] as stated in `b`, just that the text after the `/` is not affected. –  May 16 '15 at 11:53

2 Answers2

2
var a = {
"test123": "A",
"testing/test": "B",
"notest": "C"
};

var b = {
"test123": "Test 123",
"testing": "Testing"
};

var c = {};
for (var p in a) {
  var prop = p.split("/")[0];

  if (b.hasOwnProperty(prop)) {
    c[p.replace(prop, b[prop])] = a[p];
  } else {
    c[p] = a[p];
  }
}
console.log(c);

http://plnkr.co/edit/Lhi5fLKkW4UBzhOK6le7?p=preview

yozh
  • 1,213
  • 2
  • 10
  • 18
  • 1
    Hi, while your code worked well for this example, with much larger objects, the order is messed up... do you know how to do this on php? Thanks :) –  May 17 '15 at 02:20
  • Do you mean order of properties? It cannot be guaranteed, see [this SO question](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). Don't know about PHP though. – yozh May 18 '15 at 08:01
  • In PHP, arrays have an order that is not random, unlike in Javascript. So I was hoping that you would know how to do it in PHP :) –  May 18 '15 at 09:53
1

This should do it:

var a = {
  "test123": "A",
  "testing/test": "B",
  "notest": "C"
};


var b = {
  "test123": "Test 123",
  "testing": "Testing"
};

var c = {}

for (prop in a) {
    //console.log(prop);

    var propParts = prop.split("/");  // only get the first part of properties with an "/" in it

    if(b.hasOwnProperty(propParts[0])) {  // if we have a new property name in b, use that
        c[b[propParts[0]]] = a[prop];
    } else {     // if not, use the one that came from object a
        c[prop] = a[prop];
    }
}

console.log(c);

Fiddle: http://jsfiddle.net/03ynxwa0/

EDIT:

I missed that you also want the "/" in the new propertyname. Please refer to yozh's answer!

Jeff
  • 6,895
  • 1
  • 15
  • 33
  • 1
    Welcome. For the future, please avoid questions that only ask for how can I do that, without showing your own attempt to solve it. You could've quite easily found a way to do that on your own with googleing! – Jeff May 16 '15 at 12:16
  • This actually does not correctly solve the problem, the slash is gone. – yozh May 16 '15 at 12:23
  • @yozh Yes, you're right. I missed that. Thanks for noticing! – Jeff May 16 '15 at 12:26