1

I have 3 sets of Json data

data1 [{id:1, systemName: "foo", moreInfo: "blah},
       {id:2, systemName: "boo", moreInfo: "blahBlah}]


data2 [{systemName: "foo",count: 21},
       {systemName: "boo",count: 454}]

data3 [{systemName: "foo",count: 643},
       {systemName: "boo",count: 674}]

what I require is to take the counts from data 2 and 3 and add them to their respective objects in data1 with new name like data2Count and Data3Count

so data1 would look like this:

data1 [{id:1, systemName: "foo", moreInfo: "blah, data2Count: 21, data3Count: 643},
       {id:2, systemName: "boo", moreInfo: "blahBlah, data2Count: 454, data3Count: 674}]

Currently I can only achieve this:

data1 [{id:1, systemName: "foo", moreInfo: "blah"}
       {id:2, systemName: "boo", moreInfo: "blahBlah"} data2Count: 454]

Using:

angular.forEach(data1, function(value1, key1) {
    angular.forEach(data2, function(value0, key0) {
        if (value1.systemName === value1.systemName) {
            $scope.sysFlow.data2Count = value1.count
        }
    })
})

1 Answer 1

1

You can use Array#map to do something like this:

var data1 = [
    {id:1, systemName: "foo", moreInfo: "blah"},
    {id:2, systemName: "boo", moreInfo: "blahBlah"}
  ]


var data2 = [
    {systemName: "foo",count: 21},
    {systemName: "boo",count: 454}
  ]

var data3 = [
    {systemName: "foo",count: 643},
    {systemName: "boo",count: 674}
  ]
  
data1 = data1.map(function(obj) {

  obj.data2Count = data2.filter(function(o) {
    return o.systemName === obj.systemName
  })[0].count;

  obj.data3Count = data3.filter(function(o) {
    return o.systemName === obj.systemName
  })[0].count;

  return obj;
})

console.log(data1)

Sign up to request clarification or add additional context in comments.

2 Comments

mm @tan few issues, well its an issure with our version of chrome that we cant use => front end (doh) how do I edit the change th aboue to use regular ``function``` ??
Thanks very much I was missing the return haha, kept creating an array haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.