score:1
Accepted answer
Under the assumption that your data is so well groomed, a quick and dirty way is to combine the object's values into strings and make a Set
out of them. Set
s, by nature, cannot have duplicates. But at the same time, JavaScript Set
s can only understand duplicates that are primitive data types (numbers, strings, etc.)
Concat the values, turn it into a Set
, turn it back into the data structure it was previously.
Please tell me if you find anything confusing about my syntax. I make liberal use of modern JavaScript syntax.
Edit: You can use Array#filter
to be selective as to what should exist in the array.
const values = [
{
"name": "this_year",
"node": "NA"
},
{
"name": "this_year",
"node": "1"
},
{
"name": "this_year",
"node": "4"
},
{
"name": "this_year",
"node": "2"
},
{
"name": "this_year",
"node": "4"
},
{
"name": "this_year",
"node": "1"
}
]
function constructUniques(array) {
const concattedStringValues = array
.filter(({node}) => node !== 'NA')
.map(({name, node}) => `${name} ${node}`)
const uniqueStrings = new Set(concattedStringValues)
return [...uniqueStrings].map(concattedVal => {
const [name, node] = concattedVal.split(' ')
return {name, node }
})
}
console.log(constructUniques(values))
Source: stackoverflow.com
Related Query
- Remove duplicates from parsed CSV array
- Copy data from csv into array in D3
- how to get data with tsv or csv to array in d3.js from a txt file?
- Combine Nodes & Links in D3 Force Graph After CSV is Parsed to make one Array (Angular & D3.JS)
- Remove Duplicates from D3js Dropdown
- d3 filter duplicates names from csv
- new array from csv data
- How to create an array of nested objects from a csv file in D3.js?
- Creating array from csv with d3.group without group headers
- Javascript - Can not access array elements of an array created from a csv file
- Add new row dynamically to an array with headers created from a csv file
- d3. Calling a csv url from an array javascript
- Remove specific value from array
- Javascript D3: from csv to array
- Create an array from csv for use with D3.js library
- How can I parse csv data from a javascript array of strings for use in d3 graphs
- How do I retrieve an array from a csv file in javascript using d3?
- D3.js creating nested array from csv
- filter data from csv using array filter not working as expected
- How do I remove all children elements from a node and then apply them again with different color and size?
- D3js take data from an array instead of a file
- Remove end-ticks from D3.js axis
- csv to array in d3.js
- How to load data from a CSV file in D3 v5
- Importing data from multiple csv files in D3
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- Cannot import data from csv file in d3
- Select data from a CSV before loading it with javascript (d3 library)
- Returning array from d3.json()
- D3.js: Remove force.drag from a selection
More Query from same tag
- c3js convert xAxis count from decimal to Integer
- D3.js Process Diagram
- D3 collapsible tree - jumpy on zoom
- Controlling d3js pie animations and place labels inside pie
- Changing a Json Array in D3 for Bar Chart
- Pie chart drop-shadow or 3D d3.js
- Modifying a Force Layout - using indexes rather than references gives TypeError
- D3js - can't change style for svg text elemets
- Plotting Name of States on Map using Node js and D3 in real time
- D3 doesn't load a CSV file from an external URL
- Providing X axis with proper tick values
- D3.js Combining Candlestick Chart with Line Graph
- D3 updating data on parent element wont reflect on child elements
- Creating nested Json structure with multiple key values in Python from Json
- What is the best way to match elements by index for update in a Scatter plot?
- Making a line with a different data format
- d3.js label bars of bar chart
- How to display and hide a tooltip message on mouseenter and mouseleave respectively?
- d3.js tween factory return function applied to non-interpolable property values
- Scaling SVG coordinates, but not elements
- Limit drag movements to a circular SVG boundary
- foreignObject bug with Chrome with d3?
- How to different colours and legend in d3 js donut chart?
- Add two different svg element in g element of svg using d3.js from a dataset
- stacked bar chart has white space in between, y0 starting at wrong spot?
- Stratify flat data without directly specifying parent
- angular.js $apply bottleneck
- How can I bring a circle to the front with d3?
- D3.js node color based on type
- Convert d3 event from Javascript to Typescript (Angular2)