0

I want to insert values in to object based on id inside that object.

First Object:

{
    "topics": [{
        "id": 131,
        "topicId": "1485853106269",
        "title": "Topic Title",
        "details": "topic details",
        "username": "ki****@gmail.com",
        "userImage": "assets/img/spiritual-icon4.png",
        "dayPosted": "1/31/2017, 5:16:53 PM"
    }, {
        "id": 132,
        "topicId": "1485863413654",
        "title": "Check",
        "details": "topic details",
        "username": "ki****@gmail.com",
        "userImage": "assets/img/spiritual-icon4.png",
        "dayPosted": "1/31/2017, 5:20:13 PM"
    }, {
        "id": 133,
        "topicId": "1485945328280",
        "title": "New Topic",
        "details": "Sample Topic",
        "username": "ki****@gmail.com",
        "userImage": "assets/img/spiritual-icon4.png",
        "dayPosted": "2/1/2017, 4:05:28 PM"
    }, {
        "id": 134,
        "topicId": "1485945483238",
        "title": "New Topic2",
        "details": "New Topic2",
        "username": "ki****@gmail.com",
        "userImage": "assets/img/spiritual-icon4.png",
        "dayPosted": "2/1/2017, 4:08:03 PM"
    }],
    "role": "ROLE_ADMIN"
}

Second Object:

{
   "1485594764668": 1,
   "1485853106269": 2,
   "1485945483238": 1
}

I want to insert values from second object into first object based on the matching property "topicId": "1485853106269".

For example: The value of "topicId": "1485853106269" in first object matches with a key inside second object so result would be like below with added value "count": 2:

{
    "id": 131,
    "topicId": "1485853106269",
    "title": "Topic Title",
    "details": "topic details",
    "username": "ki****@gmail.com",
    "userImage": "assets/img/spiritual-icon4.png",
    "dayPosted": "1/31/2017, 5:16:53 PM",
    "count": 2 //ADDED here
}
kittu
  • 6,662
  • 21
  • 91
  • 185
  • 1
    well what did you tried? But theory is loop through your objects and your second object properties check if anything match and if it does, add the value ;). Here is how you can loop through object properties: http://stackoverflow.com/questions/8312459/iterate-through-object-properties – xszaboj Feb 01 '17 at 14:50
  • For the second object, I could do `for(key in object)` and for the first object `forEach` but unable to figure out how to mix both to get the desired result – kittu Feb 01 '17 at 14:52

2 Answers2

2

A simple each loop will do:

obj.topics.forEach(item => item.count = counts[item.topicId])
Seiyria
  • 2,112
  • 3
  • 25
  • 51
  • @Andreas you sure about that? I've written this same code many times. – Seiyria Feb 01 '17 at 14:58
  • @Andreas Whoops. I forgot to `return prev` - either way, OP already had the count object so it was unnecessary. – Seiyria Feb 01 '17 at 14:59
  • 1
    You should check if `counts[item.topicId]` exist first. This could assign `undefined` as `item.count`. – ibrahim mahrir Feb 01 '17 at 14:59
  • @ibrahimmahrir I don't see that as explicitly necessary, personally. – Seiyria Feb 01 '17 at 15:00
  • 1
    TO: "_I want to insert values from second object into first object **based on the matching property**_" -> No match = no `counts` – Andreas Feb 01 '17 at 15:01
  • @Andreas Just about to paste that hhhh. – ibrahim mahrir Feb 01 '17 at 15:01
  • Right, and undefined is roughly equivalent to no `count`. the undefined check is strictly unnecessary because any comparisons you will do will either be if(counts) or if(counts > 0) or something like that, to which undefined will evaluate to 0. – Seiyria Feb 01 '17 at 15:03
  • I am new to javascript and I still don't understand es6 syntax (`=>`). Thank you for the answer anyway. I will try to learn and understand the syntax – kittu Feb 01 '17 at 15:03
  • 1
    Checking properties against `undefined` is the right way to check if it exist or not. If in the context of this question, it's not necessary, OP could learn from it. – ibrahim mahrir Feb 01 '17 at 15:06
2

var obj = {"topics":[{"id":131,"topicId":"1485853106269","title":"Topic Title","details":"topic details","username":"ki****@gmail.com","userImage":"assets/img/spiritual-icon4.png","dayPosted":"1/31/2017, 5:16:53 PM"},{"id":132,"topicId":"1485863413654","title":"Check","details":"topic details","username":"ki****@gmail.com","userImage":"assets/img/spiritual-icon4.png","dayPosted":"1/31/2017, 5:20:13 PM"},{"id":133,"topicId":"1485945328280","title":"New Topic","details":"Sample Topic","username":"ki****@gmail.com","userImage":"assets/img/spiritual-icon4.png","dayPosted":"2/1/2017, 4:05:28 PM"},{"id":134,"topicId":"1485945483238","title":"New Topic2","details":"New Topic2","username":"ki****@gmail.com","userImage":"assets/img/spiritual-icon4.png","dayPosted":"2/1/2017, 4:08:03 PM"}],"role":"ROLE_ADMIN"};

var countObj = {
    "1485594764668": 1,
    "1485853106269": 2,
    "1485945483238": 1
};

obj.topics.forEach(function(o){
     var c = countObj[o.topicId];
     if(c !== undefined)
         o.count = c;
});

console.log(obj);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73