1

I have read through a number of Stack Overflow questions but none appear to be relevant to the problem I'm trying to solve.

I have an array of objects that I'm saving in localStorage which looks like this (this example includes just two):

[
  {
    "image": "http://example-image.jpg",
    "restaurantName": "Elena's L'Etoile",
    "parentLocation": "London",
    "areaLocation": "West End of London",
    "pageLink": "http://example-address1"
},
  {
    "image": "http://example-image2.jpg",
    "restaurantName": "Pied a Terre",
    "parentLocation": "London",
    "areaLocation": "West End of London",
    "pageLink": "http://example-address2"
  }
]

Each time a user visits a page, data is pulled from the page, and a restaurant object is created which looks like this:

 var restaurant = {"image": $image, "restaurantName": $restaurantName, "parentLocation": $parentLocation, "areaLocation": $areaLocation, "pageLink": $pageLink};

This is then stored pushed into the existing array of objects (above) with:

existingRestaurants.push(restaurant);

The problem is that if the user visits the same page twice, duplicate objects are pushed in the array. How can I ensure that only unique objects are pushed into the array?

Approaches I've looked into: using $.each, $.inArray, $.grep. I thought that the simplest way would be to loop through all the objects in the existingRestaurants array and compare the value of the "restaurantName" key with the corresponding value in the new restaurant object.

But I haven't been able to find anything else similar on Stack Overflow.

4
  • 1
    Switch from an Array to an Object with the restaurant name or page link as the keys. Commented Feb 18, 2016 at 16:07
  • Show your attempts using $.grep or $.each so we can help point out problem. You were probably trying to compare two objects that don't share same reference. $.inArray would definitely not work for this Commented Feb 18, 2016 at 16:13
  • .filter or .some could be used to check if the object already exists. No need for jquery related stuff. some Commented Feb 18, 2016 at 16:14
  • @JohannesJander is it not possible to keep the overall structure of the data as an array to achieve what I want to do? Commented Feb 18, 2016 at 16:14

2 Answers 2

1

There's a few solutions you could use here. The first is to keep your current array of objects and scan them all for a duplicate restaurant name before inserting a new one. This would look something like this:

// assuming 'arr' is the variable holding your data
var matches = $.grep(arr, function(obj) {
    return obj.restaurantName == $restaurantName;
});

if (matches.length) {
    console.log('Duplicate found, item not added');
} else {
    var restaurant = {
        "image": $image,
        "restaurantName": $restaurantName,
        "parentLocation": $parentLocation,
        "areaLocation": $areaLocation,
        "pageLink": $pageLink
    };
    arr.push(restaurant);
}

Working example

Alternatively, and preferably, you could amend your data structure to be an object with the keys being the value which cannot be duplicated; in this case the restaurant names:

var arr = {
    "Elena's L'Etoile": {
        "image": "http://example-image.jpg",
        "parentLocation": "London",
        "areaLocation": "West End of London",
        "pageLink": "http://example-address1"
    },
    "Pied a Terre": {
        "image": "http://example-image2.jpg",
        "parentLocation": "London",
        "areaLocation": "West End of London",
        "pageLink": "http://example-address2"
    }
};

if (arr[$restaurantName]) {
    console.log('Duplicate found, item not added');
} else {
    var restaurant = {
        "image": $image,
        "parentLocation": $parentLocation,
        "areaLocation": $areaLocation,
        "pageLink": $pageLink
    };
    arr[$restaurantName] = restaurant;
}

Working example

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

2 Comments

Thanks for your help Rory! I think I'll go for the first one cos the existing structure might be important for me later in the project. Can I ask why second example is preferable?
No problem, glad to help. It's because the prevention against duplicates is ingrained in to the keys of an object, so the code is shorter and faster as it's a simpler task to check for an existing entity.
0

How about an associative array? You'll have to select a key though:

var restaurant0 = {"image": "http://example-image.jpg", "restaurantName": "Elena's L'Etoile", "parentLocation": "London", "areaLocation": "West End of London", "pageLink": "http://example-address1" };
var restaurant1 = {"image": "http://example-image2.jpg", "restaurantName": "Pied a Terre", "parentLocation": "London", "areaLocation": "West End of London", "pageLink": "http://example-address2"};

var existingRestaurants = {};
existingRestaurants["id0"] = restaurant0;
existingRestaurants["id1"] = restaurant1;

Comments

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.