-2

I have two arrays as given below.

var obj1 = [
        {
            "POST": "Developer",
            "USERNAME": [
                "User1",
                "User2"
            ]
        },
        {
            "POST": "Tester",
            "USERNAME": [
                "User3"
            ]
        }
    ]

var obj2 = [
        {
            "USERNAME": "User1",
            "USER_PASSWORD": "test1"
        },
        {
            "USERNAME": "User2",
            "USER_PASSWORD": "test2"
        },
        {
            "USERNAME": "User3",
            "USER_PASSWORD": "test3"
        }
    ]

From obj1 I want the POST value to be appended to the appropriate user in obj2.

Final result should look like the below array.

[
    {
        "USERNAME": "User1",
        "USER_PASSWORD": "test1",
        "POST": "Developer"
    },
    {
        "USERNAME": "User2",
        "USER_PASSWORD": "test2",
        "POST": "Developer"
    },
    {
        "USERNAME": "User3",
        "USER_PASSWORD": "test3",
        "POST": "Tester"
    }
]
JJJ
  • 32,902
  • 20
  • 89
  • 102
user87267867
  • 1,409
  • 3
  • 18
  • 25
  • 3
    What have you tried so far? You should post that code and we can try and help you. If you've not tried anything, please go and try something and then come back if you run into any problems. – Andy Jul 11 '14 at 10:03
  • possible duplicate of [What is the most efficient way to clone an object?](http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object) – fmsf Jul 11 '14 at 12:42

2 Answers2

1

Try this:

var obj1 = [
                {"POST":"Developer","USERNAME":["User1","User2"]},
                {"POST":"Tester","USERNAME":["User3"]}
            ];
            var obj2 = [
                {"USERNAME":"User1","USER_PASSWORD":"test1"},
                {"USERNAME":"User2","USER_PASSWORD":"test2"},
                {"USERNAME":"User3","USER_PASSWORD":"test3"}];
            var obj3=[];

            obj1.forEach(function(eachObj1) {
                obj2.forEach(function(eachObj2) {
                    if(eachObj1.USERNAME.indexOf(eachObj2.USERNAME) >= 0) {
                        console.log("in if")
                        eachObj2.POST = eachObj1.POST;
                        obj3.push(eachObj2);
                    }
                });
            });
Kaushick Gope
  • 154
  • 1
  • 11
-2

try this,

var obj1 = [{"POST":"Developer","USERNAME":["User1","User2"]},{"POST":"Tester","USERNAME":["User3"]}];
var obj2 = [{"USERNAME":"User1","USER_PASSWORD":"test1"},{"USERNAME":"User2","USER_PASSWORD":"test2"},{"USERNAME":"User3","USER_PASSWORD":"test3"}];
$.each(obj2,function(i,val){
   $.each(obj1,function(index,innerVal){

        var post=innerVal.POST;
        $.each(innerVal.USERNAME,function(i,user){
            if(user==val.USERNAME){
                val.POST=post;
            }
        });
    });
});
alert(JSON.stringify(obj2));

Demo

jomon
  • 112
  • 5
  • Since the post is tagged with Node.js it's unlikely that they have jQuery included. And including jQuery just to loop through arrays wouldn't be reasonable. – JJJ Jul 11 '14 at 11:00