Jose the hose
Jose the hose

Reputation: 1895

Creating a json object by comparing another two json objects

I am trying to create a new json object (selectedProductAttributes) that is created when the contents of a 2nd json object (selectedProductAttributesNames) are compaired against a 3rd object (allProductAttributes)! To explain in more detail I have some examples below

The first one called allProductAttributes is a json object that contains differnt attributes for form fields

$scope.allProductAttributes = [
        {
            name: 'postcode',
            title: 'Postcode',
            required: true,
            type: 'text',
            max: '10',
            placeholder: 'Enter a postcode'
        },
        {
            name: 'Term',
            title: 'Contract term',
            required: true,
            type: 'number',
        },
        {
            name: 'bandwidth',
            title: 'bandwidth',
            type: 'select',
            options: [
                {id: 10, name: '10 mb/s'},
                {id: 100, name: '100 mb/s'},
                {id: 1000, name: '1000 mb/s'},
                {id: 10000, name: '10000 mb/s'}
            ]
        },
        {
            name: 'service',
            title: 'Service',
            required: true,
        }

]

The next json object, selectedProductAttributesNames, contains a list of the form fields that the user wants to select from allProductAttributes

$scope.selectedProductAttributesNames = [
"postcode",
"service"
]

So from the above example, when the user requests 'postcode' and 'service' in selectedProductAttributesNames, a new json object should be created containing the form field attributes from allProductAttributes...

$scope.selectedProductAttributes = [
    {
            name: 'postcode',
            title: 'Postcode',
            required: true,
            type: 'text',
            max: '10',
            placeholder: 'Enter a postcode'
        },
    {
            name: 'service',
            title: 'Service',
            required: true,
        }
]

I'm sorry if I have confused you. Does anyone know how I can achieve this? Thanks

Upvotes: 0

Views: 61

Answers (2)

laruiss
laruiss

Reputation: 3816

Unless you want to reinvent the wheel, you could use underscore and do something like that :

$scope.selectedProductAttributes = _.compact($scope.allProductAttributes.map(function (item) {
    return _.contains($scope.selectedProductAttributesNames, item.name) ?
            item : false}))

If you don't care about supporting IE 6, 7 nor 8, and don't want to use any external library, you could do this:

$scope.selectedProductAttributes = $scope.allProductAttributes.filter(function (item) {
    var validated = false, i, length = $scope.selectedProductAttributesNames.length;
    for (i = 0 ; i < length; i++) {
        if (item.name == $scope.selectedProductAttributesNames[i])
            validated = true;
    }
    return validated
})

Upvotes: 2

Steve Mitcham
Steve Mitcham

Reputation: 5313

If you have the ability to change the attribute source object a bit to match the following style, using dictionary style access to your objects and a loop will get you what you are looking for.

$scope.selectedProductAttributeNames = {
   'postcode' : {
      'name': 'postcode',
      /// remaining things here
   },
   /// remaining attributes here
};

for (var i = 0, l= $scope.selectedProductAttributeNames.length; i < l; i++) {
  $scope.selectedProductAttributes[x[i]] =    
     $scope.selectedProductAttributeNames[x[i]];
}

Upvotes: 0

Related Questions