score:3
Accepted answer
var input=[{student:"Alex",subject:"maths",marks:"85",answer:"Moderate"},{student:"Alex",subject:"maths",marks:"85",answer:"Moderate"},{student:"Alex",subject:"maths",marks:"85",answer:"Moderate"},{student:"Alex",subject:"science",marks:"82",answer:"Severe"},{student:"Alex",subject:"science",marks:"82",answer:"Severe"},{student:"Alex",subject:"science",marks:"82",answer:"Severe"},{student:"Alex",subject:"computer",marks:"90",answer:"Extreme"},{student:"Alex",subject:"computer",marks:"90",answer:"Extreme"},{student:"Alex",subject:"computer",marks:"90",answer:"Extreme"},{student:"john",subject:"maths",marks:"90",answer:"Extreme"},{student:"john",subject:"maths",marks:"90",answer:"Extreme"},{student:"john",subject:"maths",marks:"90",answer:"Extreme"},{student:"john",subject:"science",marks:"85",answer:"Moderate"},{student:"john",subject:"science",marks:"85",answer:"Moderate"},{student:"john",subject:"science",marks:"85",answer:"Moderate"},{student:"john",subject:"computer",marks:"87",answer:"Extreme"},{student:"john",subject:"computer",marks:"87",answer:"Extreme"},{student:"john",subject:"computer",marks:"87",answer:"Extreme"}];
//find all unique inputs, i.e. entry with unique strudent and subject
var uniqueInput = [...input].reduce((unique, obj) => {
if (unique.filter(({student,subject}) => student === obj.student && subject === obj.subject /* && otherKeyFeatureUsedForUniqueness */).length === 0) return [...unique, obj]; //if not already inserted
return unique; //already inserted
}, [])
var finalArray = [...uniqueInput].reduce((alreadyInsertedStudent, currentStudent) => {
//get array of students without current student
var arrWithoutObj = alreadyInsertedStudent.filter(({student}) => student !== currentStudent.student /* && the student or class or other properties which is constant across the duplicated rows in original input array, you can add here*/);
//if the student is not present, then withoutArray === currentarray
//if yes, then insert the student and subject
if (arrWithoutObj.length === alreadyInsertedStudent.length) return [...alreadyInsertedStudent, {student: currentStudent.student, [currentStudent.subject]: currentStudent.marks, answer: currentStudent.answer/*, ...rest additional unique row defining Properties of final answer, you can add here*/}];
//if student already inserted, then add the subject marks to that student
return [...alreadyInsertedStudent].map((prev) => prev.student === currentStudent.student ? {...prev, [currentStudent.subject]:currentStudent.marks} : prev);
}, [])
console.log(finalArray)
Source: stackoverflow.com
Related Query
- Merge properties of objects within a array together using values and remove duplicate
- Using setState to change multiple values within an array of objects -- ReactJS
- How to extract only values from array of objects and put it to an array using react and javascript?
- react js filter through an array of objects and compare values within to get the closest value to a variable
- Check if array of objects contains multiple values from another array of objects and remove duplicates
- How to set and access values of nested array objects in React state using useState hook?
- How to loop through properties of objects inside of an array within a React component?
- How to correctly add and remove specific items from an array within state?
- How can I send a file within an array of objects using formData.append()
- Loop over an array of objects and format values
- Sorting array of objects using name propery which contains alphabet and numbers using
- React merge immutable array of objects using React Immutability helpers or Immutable js
- Using a value to compare against an array of objects to return a different value within that object?
- How to iterate through array of objects and execute a request using react?
- How to check the values of an array and delete some properties based on the condition?
- Destructuring object properties from an array of objects and rendering those properties into an array
- Sum total and make an array which count by same values from array of objects
- Mapping and Reducing an Array within an Array of Objects in Javascript
- Looping over an array of objects and showing values based on static keys defined javascript
- How do I iterate though an array of objects with keys and values in react and render them in the JSX( Both keys and Values)
- React/Javascript - Render a List of Values Based On Same Object Keys Within an Array of Objects
- Find Numeric Values among string from Array of Objects and do calculation
- Javascript filter Array of objects with multiple conditions and values
- How to remove specific element from a array or object using ES6 spread operators and rest operators
- How to loop through an array an put create an array of objects using react and javascript?
- Accessing an array within an array using React, Gatsby and GraphQL
- How to process JSON Array of objects using Key values in JavaScript
- How to find the count of some object property in array of objects using react and javascript?
- Filtering an object based on key, then constructing new array of objects based on the keys and values
- Get the component from array of objects and add new properties to it - React
More Query from same tag
- Error [ERR_HTTP_HEADERS_SENT] Cannot set headers after they are sent to the client
- How to make sure that the line down is run after the function above is executed completely in javascript
- Implementing React on RoR project
- How to call a function from action before dispatching to reducer?
- How can i update select option from state in react.js
- how to pass string params in Get call of API using axios in Reactjs
- Hot to get value of button in react
- MUI DatePicker show only month, and not year or day
- Some functions not binding to props in React
- React, I cant seem to turn each movieID into the movieId's provider Link
- How to adjust valid input check method for SpinButton?
- Issue with Router, Redirect and authentication context in React
- Concatenate all values in Select of Antd
- ReactJS onClick state change one step behind
- Using setState inside useEffect on updating forms
- Why is there a double call in this React function?
- ionic globalization with react and web
- Access a React component method / state from outside the component
- Firebase .push() attempting to add to DB, but fails?
- React .trim() is not a function
- Call React Component Function from onclick in dangerouslySetInnerHTML
- React - passing 'this' as a prop
- how to add commas between names in an array using join method-Javascript
- In reactjs, how do I map an array and add to the return values?
- React - How to target a specific component instance
- Pushstate and popstate not working with condtionally rendered components
- Implementing an online game in react
- Fetch and sessions and CORS
- How to initialize a firebase instance in the action creator in react?
- Use component props in onClick redux-thunk dispatch React-Redux