Using a loop to eliminate duplicate values within a JSON dataset

Check out this link:

https://jsfiddle.net/3fmp43db/

data = [{
  city: 'Mushroom Kingdom',
}, {
  city: 'Mushroom Kingdom',
}, {
  city: 'Mushroom Kingdom',
}, {
  city: 'Mushroom Kingdom',
}, {
  city: 'Mushroom Kingdom',
}, {
  city: 'Mushroom Kingdom',
}, {
  city: 'Planet Zebes',
}, {
  city: 'Planet Zebes',
}, {
  city: 'Planet Zebes',
}];

for(var i = 0; i < data.length; i++) {
    var obj = data[i];
    $(".test").append(obj.city + "<br>")
}   

This task seems simple, but I'm struggling to find a way to eliminate duplicate values before displaying the list.

Answer №1

modify the final for loop to:

var citiesList = {};
for(var i = 0; i < data.length; i++) {
    citiesList[ data[i].city ] = "";
}
$(".test").append(Object.keys( citiesList ).join("<br>"));

view the updated example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attach a pushpin to the Bing map

After successfully adding push pins to a Bing map using a REST API, I encountered an issue where more than 100 push pins are not visible on the map. The REST API is functioning correctly in fetching longitude and latitude for the map's push pins. Howe ...

Generating XML attribute through XPath

How can I create an XML attribute using XPath to find the element I want to add the query on? For example: const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng&quo ...

Combining two objects using three.js is not possible due to concatenation

I am attempting to combine two objects (bar and circle) to create a ping pong racket in three js, Even after using var parent = new THREE.Object3D();, the objects remain separate This is the code for my function : function addPaddle() { var paddl ...

Tips for resizing mesh along the global axis

Have you ever used an online 3D editor that allows you to manipulate individual meshes by moving, scaling, and rotating them? This editor utilizes custom transform controls inspired by the TransformControls code from three.js. Here is a snippet of code fro ...

How do I link a pre-determined value to a data point using Highcharts?

For example, consider a scenario where a dynamic chart contains values that need to be displayed in different formats for tooltips. Is it possible to pass a pre-formatted value along with the raw numeric value in some way? For instance, in a sales Pie cha ...

How to use Express Validator to validate both email and username within a single field?

I am currently developing an application using the Express (Node.js framework) and I want to allow users to log in with either their email address or username. My question is, how can I implement validation for both types of input on the same field using e ...

Can the rounded corners created by the CSS property "border-radius" be made non-clickable?

Can the rounded borders created by "border-radius" be made unclickable and undetectable when hovering over them? ...

Is it possible to incorporate multiple IDs into the changeImage function for a radio

I've encountered an issue with my function that changes images based on radio button selections. It works fine for one image slider, but now I want to make it available for a second slider. The problem is that when I use the radio buttons for the seco ...

Issue encountered while retrieving information from Mysql database using JSON format in conjunction with PHP

I am currently diving into the world of building android applications and I could use some assistance with retrieving data from a MySQL database. I attempted to follow this particular tutorial to achieve this task, but encountered the following error: E ...

Adding images to your SVG using Bobril is a simple process that can add visual

I have been attempting to insert an image into an SVG using Bobril, but the following code is not functioning as expected: { tag: 'svg', children: { tag: 'image', attrs: { 'xlink:href': &ap ...

Difficulty retrieving JSON object with AngularJS

I am having trouble extracting information from a JSON string that I have (see image below). { all: [{ info: { name: "Fahad", lat: "41.954815", lon: "-87.647209" } }, { info: { ...

What category does React.js fall under - library or framework?

Hey there! I've noticed that sometimes people refer to React JS as a library, while others call it a framework. Can you shed some light on which term is more accurate? ...

Re-sorting with _.sortBy() eliminates additional 0s from decimal values (transforming 0.10 to 0.1 and beyond)

Here is an array that needs to be sorted: var baseBetAmount = [ { val: 'OtherBaseBet', text: 'Other' }, { val: 0.10, text: '$0.10' }, { val: 0.20, text: '$0.20' }, { val: 0.50, text: ...

The Carousel Slider malfunctions when attempting to set up multiple carousels

I recently created a carousel slider that seems to be behaving incorrectly when multiple carousels are added. It should slide by one item at a time. For instance, with only one carousel, it slides by one item; with two carousels shown, it slides by two ite ...

"SyntaxError: import statements can only be at the top level of a module" is the error message that I keep encountering.`

For the complete code, click here: https://github.com/vscodr/axios_issue After spending some time away from JavaScript working in Python, I decided to come back and try to tackle similar tasks using JS. However, I seem to be stuck at a very basic issue! D ...

Error: The data type java.lang.String cannot be converted to a JSONArray in this context

org.json.JSONException: Value NeededTypes of type java.lang.String cannot be converted to JSONArray I encountered this issue while attempting to retrieve the JSONArray neededTypesArray. If you have any suggestions on how to resolve it, I would greatly app ...

Exploring the JSON structure

Struggling to come up with a solution as I am limited by certain restrictions. I need to create a mobile navigation system using a JSON object provided by the proprietary server. The only allowed framework is jQuery 1.12.4, no other frameworks or updated v ...

The functionality of CKEditor is not compatible with PopUp windows

Currently, I am using CKEditor along with the bPopUp jQuery plugin. When I set up CKEditor in the HTML without any additional scripts, it functions perfectly. However, when I try to embed it into a pop-up window, it stops working correctly. I can see the C ...

Are you encountering difficulties while managing back pressure as anticipated when applying node request or axios in combination with streams for downloading and extracting files?

We have encountered a peculiar problem while trying to download and decompress a large file of approximately 6 GB (which decompresses to around 64 GB) using the HTTP protocol. To achieve this, we are utilizing either Node.js' request library or axios. ...

Whenever I include an onClick event to a div element, the entire webpage fails to display

Currently taking on the task of developing a seat booking website. I am encountering an issue with adding an event listener to a particular div element, which should trigger a function to store the value of the div. However, upon implementing the onClick e ...