score:2
Accepted answer
you are using set
as the return value for a filter function. is it really intended that way?
given the data:
const data = [
{
"name": "ankh of anubis",
"rank": {
"_type": "medal",
"current": "ankh-of-anubis"
}
},
{
"name": "bonus roulette",
"rank": {
"_type": "medal",
"current": "bonus-roulette"
}
},
{
"name": "jetx",
"rank": {
"_type": "medal",
"current": "jetx"
}
},
{
"name": "gates of olympus",
"rank": {
"_type": "trophy",
"current": "gates-of-olympus"
}
},
]
you can do this:
const uniquevalues = new set();
data.foreach(record => uniquevalues.add(record.rank._type));
console.log(uniquevalues);
here's the link.
score:0
solution similar to your first try :
const data = [{"name":"ankh of anubis","rank":{"_type":"medal","current":"ankh-of-anubis"}},{"name":"bonus roulette","rank":{"_type":"medal","current":"bonus-roulette"}},{"name":"jetx","rank":{"_type":"medal","current":"jetx"}},{"name":"gates of olympus","rank":{"_type":"trophy","current":"gates-of-olympus"}},];
const uniquevalues = new set(data.map(elem => elem.rank._type));
uniquevalues.foreach(value => console.log(value));
score:0
const data = [
{
"name": "ankh of anubis",
"rank": {
"_type": "medal",
"current": "ankh-of-anubis"
}
},
{
"name": "bonus roulette",
"rank": {
"_type": "medal",
"current": "bonus-roulette"
}
},
{
"name": "jetx",
"rank": {
"_type": "medal",
"current": "jetx"
}
},
{
"name": "gates of olympus",
"rank": {
"_type": "trophy",
"current": "gates-of-olympus"
}
},
]
const result = data.filter((item, index) => {
const itemindex = data.findindex(i => i.rank._type === item.rank._type)
return itemindex === index
})
console.log(result)
score:0
simple way:
array.from(new set(datalist.map(i => i.name)))
score:1
assuming your array is called data
:
const unique = [...new set(data.map(item => item.rank._type))];
Source: stackoverflow.com
Related Query
- how to get unique values from a list of objects?
- How to get values from one array of objects into another array of objects
- How to store values from a list into an array of objects in ReactJS
- How get to All values matching the query from mongodoDb at Once from multiple documents having nested array of objects
- How get multiple values from checkbox list into state using React useState hook
- how to get min or max dates from a list of dates using moment.js?
- How to get values from input types using this.refs in reactjs?
- How to get values from child components in React
- How to count number of occurrences of distinct values from an array of objects in Javascript?
- How to get values from react FieldArray in formik form with other fields?
- How to get access to CSS values from a styled component (React)?
- How to get the values from a Multiple Search Selection dropdown box in react-semantic-ui?
- React remove object from list of objects without unique value, using hooks
- How can I get list of all types from graphql schema?
- How to get Current week from given array of objects date in javascript
- How to get form values from multiple child components?
- How do I create an array of unique field values in React with data from Firestore?
- React: How to get values from Material-UI TextField components
- get all values from array of objects in react typescript
- How do I use Redux-form to get values from checkboxes?
- How to get default values from Typescript object?
- How to get array of objects in javascript from an array of dictionaries in python?
- How to iterate through a list of objects from api and render it to jsx page React functional component
- how to get Unique values of each column in Mongodb queryset using nodejs
- How to Calculate sum of values from objects insidte nested array
- How can I get the selected item from List in React?
- how to filter object from nested objects by specific unique key
- Get unique category property values from an array
- How to add a filter in react-admin List populated by unique values within the list?
- How to get label values dynamically and adding the numbers together from different input with reactjs
More Query from same tag
- React data after .Map
- Render JSON data (from reddit API) with reactjs
- Retrieve X-Request-Id from response header in React App
- ESLint issue in the importing firebase in React app
- confusing express mongo behavior. mongo express get method (filter by email) not working
- Filter response from api on user search getting error :`Unhandled Rejection (TypeError): Cannot read property 'results' of undefined`
- ReactJs Image Not found on production mode
- React async componentWillUnmount
- Cannot GET / expo react
- React undefined setState from props
- How to change default error message in Yup
- Handling undefined/null properties in components during first render
- Clicking link from focused side nav opens page scrolled down
- How to reassign/replace a value of an array by index if its in a global state
- Create a two items per row list from array
- Why is metro bundler taking so long to load my app?
- Filtering data causing an error called "Pasring error: Unexpected Token"
- How to change button background color and text color using setSate in react?
- Are react stateless components equal to ReacDOM.PureComponents
- Google maps react have HeatMap?
- How to parse mixed content into React components?
- Type error on React Route component
- Is React context provider not mounted before child components?
- CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model
- React Hot Reload with Redux
- Reactjs : Nested navigation between component?
- How to pass data between the function App() parent and the Material-UI nested sidebar child
- ReactJS - Can't access properties in object from a fetch
- how to get data from localhost:9200 (elasticsearch) using axios in react
- How to vertically align button content and the icon in the same line and icon on the extreme right of button in tailwindcss?