pingin
pingin

Reputation: 484

Check if an object exists with a property set to a particular value in a nested object (array of objects)

I'm trying to construct a JSON object from a data source which should look something like this:

var allProduce = [
        {"name": "cabbage", "type": "vegetable"},
        {"name": "potoato", "type": "vegetable"},
        {"name": "carrot", "type": "vegetable"},
        {"name": "apple", "type": "fruit"},
        {"name": "orange", "type": "fruit"}
]

As I work through the data source, I want to add to the nested object 'allProduce' a new object with the corresponding entry if and only if the object doesn't already exist.

So if I have in my data source the following rows:

**Product          Type     Country origin      In stock?**
apple            fruit       Ireland             Yes
apple            fruit       France              Yes
apple            fruit       USA                 No
cabbage          vegetable   UK                  Yes
cabbage          vegetable   France              No

then the resultant 'allProduce' nested object should be:

var allProduce = [
        {"name": "apple", "type": "fruit"},
        {"name": "cabbage", "type": "vegetable"}
 ]

This is where I'm confused as I'm not sure how I check each time (without looping through the whole nested object?), whether or not the record exists. I'm doing something like this (using jQuery:

 $('.sourceData table tbody tr').each(function(i,row) {
    var fruitName = $.trim($(row).find('td:nth-child(1)').text()); 
    var type = $.trim($(row).find('td:nth-child(2)').text()); 

    if (AN OBJECT WITH THE "NAME" PROPERTY SET TO fruitName DOESN'T ALREADY EXIST) {
       allProduce.push({"name":fruitName, "type": type});

    }
 });

What should be in the brackets of the if statement, where I currently have (AN OBJECT WITH THE "NAME" PROPERTY SET TO fruitName DOESN'T ALREADY EXIST). Or is there maybe a better approach to this?

Upvotes: 2

Views: 202

Answers (2)

Antoine Combes
Antoine Combes

Reputation: 1454

You can use JQuery's filter() method:

$('.sourceData table tbody tr').each(function(i,row) {
    var fruitName = $.trim($(row).find('td:nth-child(1)').text()); 
    var type = $.trim($(row).find('td:nth-child(2)').text());

    if ($(allProduce).filter(function(index, object){return object.name == fruitName;}).length == 0) {
       allProduce.push({"name":fruitName, "type": type});

    }
 });

Upvotes: 1

Esteban Felix
Esteban Felix

Reputation: 1561

You can use an object as a HashSet to see if a certain name has been encountered. An example below:

(function () {
    var exists = {};
    $('.sourceData table tbody tr').each(function (i, row) {
        var fruitName = $.trim($(row).find('td:nth-child(1)').text());
        var type = $.trim($(row).find('td:nth-child(2)').text());

        //AN OBJECT WITH THE "NAME" PROPERTY SET TO fruitName DOESN'T ALREADY EXIST
        if (!exists[fruitName]) {
            exists[fruitName] = true;
            allProduce.push({"name": fruitName, "type": type});

        }
    });
})();

Upvotes: 1

Related Questions