Pergunta

In javascript, I have 2 maps map1 = {a:1 , b:2, c:3, d:4, e:5}; map2 = {td:a, bd:c, sd:e};

Now I need to search the values of the map2 which is (a,b,e) if it is the key of the map1 and then update the value of map2 with the corresponding value in map1 Example- map2[td] = a and map[a] = 1 then I want to update map2[td] = 1. Can anyone help me finding an algorithm.

Foi útil?

Solução

You could use a for..in loop

var key;
for (key in map2)
    if (map2[key] in map1)
        map2[key] = map1[map2[key]];

You may want to consider what happens if properties are inherited though

Outras dicas

Object.keys(map2).forEach(function (d, i) {
    map2[d] = map1[map2[d]];
})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top