score:13
Hope this oneliner helps
const arr = [{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined}]
let pp = arr.filter( (ele, ind) => ind === arr.findIndex( elem => elem.jobid === ele.jobid && elem.id === ele.id))
console.log(pp)
score:0
You simply need to filter based on both id and jobid combination. To do that you can group data by jobid and search within that jobid
const arr = [{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined}]
const temp = arr.reduce((acc, item) => {
//console.log(item.id, item.jobid);
if(acc[item.jobid]) {
//console.log(acc[item.jobid]);
const idx = acc[item.jobid].findIndex(data => data.id === item.id);
//console.log(item.jobid, idx)
if(idx === -1) {
acc[item.jobid].push(item);
}
} else {
acc[item.jobid] = [item];
}
//console.log(acc)
return acc;
}, {});
let res = []; Object.values(temp).forEach(item => res= res.concat(item));
console.log(res)
score:0
Create a tmp
variable to store the item already existed will decrease the loop, instead of search the whole array for each item in the array.
const arr = [
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined}
]
const tmp = new Map()
const rs = arr.reduce((acc, e) => {
if(tmp.has(e.id)) {
if (!tmp.get(e.id).includes(e.jobid)) {
acc.push(e)
tmp.set(e.id, [...tmp.get(e.id), e.jobid])
}
} else {
acc.push(e)
tmp.set(e.id, [e.jobid])
}
return acc
}, [])
console.log(rs)
score:2
You can simply create a map
from the array after filtering by jobid
, which will contain unique values.
let map = {};
array.map(element => {
if(!map(element.id)){
map[element.id] = element;
}
})
After this you can take the values as Object.values(map)
. This will contain unique elements.
Source: stackoverflow.com
Related Query
- how to remove duplicate objects from an array in reactjs
- ReactJs Redux: how to remove duplicate objects (with duplicate values) from an array when deciding redux state (after map, filter, etc. functions)?
- ReactJS / Javascript How to remove the duplicate array from multi dimentional array?
- how to remove duplicate objects from an Array of objects but with two unique keys
- How to remove duplicate values from an array of objects in react for select components of MUI
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- How to find length of an array of objects from ReactJS setState?
- ReactJS - How to remove an item from an array in a functional component?
- How to remove a component from an array in Reactjs
- How to remove object from Array in ReactJS after click event
- How to remove Duplicate Object value in an Array in ReactJs
- How to remove an element from an array in ReactJS Redux state, when a condition is true?
- How to remove one item from an array using filter() in ReactJS - function not working properly
- how to remove duplicate values from Array using reactjs?
- How to display array of objects in table getting dynamically from backend in ReactJS
- How to store values from a list into an array of objects in ReactJS
- How to remove an array from an object using reactjs
- ReactJs : How to display a specific data of an object from an array of objects with a click
- How to dynamically add and remove mapped array to another array when clicked and remove from arr when clicked again in reactjs
- Remove a specific item from an array of objects Reactjs
- Array of objects how do i check for deeply nested text string duplicates & remove from array?
- How to count number of occurrences of distinct values from an array of objects in Javascript?
- How to render random objects from an array in React?
- How to get byte array from a file in reactJS
- how to group array of objects by value in reactjs / javascript
- How to play one video from array at a time in ReactJS & AG Grid
- Create nested JSX list items from multi-level nested array of objects reactjs
- How can i get array of object from this data for state and count in reactjs
- How to get Current week from given array of objects date in javascript
- How to iterate through an array of objects from json in React.js
More Query from same tag
- Set state with React Shallow Rendering
- How to use localStorage with useReducer hook?
- react component constantly fetching and updating data for no reason
- How to get any component's props types?
- React router cannot route url with search string
- Styled Components Folder System
- How to pass children to a class nextjs?
- Change color of searched text in React
- How do I add column sort functionality to my table?
- Using a static query in a React class component
- In Reactjs, how to make a prop function do separate tasks and return their values according to the required condition
- Warning: A component is changing a controlled input of type file
- react date range calculate (opened business hours)
- Antd form doesn't identify input values
- Rendering image in react component
- Execute function in React with Redux after the update of the STATE made by the dispatch of an action?
- ReactJS check database for changes and update component without refreshing browser
- Why do my options created with array map method has empty values (React)?
- Row border hidden by background?
- Trying to remove a single record from the table, but it is removing the whole records from reacthooks page
- How do I return values from a React component?
- Reactjs - Updation in Reactjs build dynamically. Client wants to update hostname and port in ReactJs build. How he can do it?
- redux saga yield all cancel other effect when one failed
- REACT ERROR <th> cannot appear as a child of <thead>. See (unknown) > thead > th
- React setState doesnt update data immediately
- React Native Add Spacing Between Components
- How do I display an object from an array based on timer?
- Axios post blocked by CORS. Using CLoudinary api
- How to assign event listeners to React.useRef
- How to make material ui icon clickable and redirect to url