Mapping Javascript Object Property With Another Property In Same Javascript Object
I have a JavaScript Object and I'm looking forward to map() it's Id with another id in the same JavaScript Object, my JavaScript Object Schema is var items = [ { BossId: '03'
Solution 1:
You should be using filter
instead of map
. Map always creates an array of the same length as the original, while filtering will produce a subset:
function getItemsByBossId (items, id) {
return items.filter(item => item.BossId === id)
}
Solution 2:
You can go a step further and create a generic filter function for any property (or combination thereof) like this:
functionfilterItemsBy (items, filter = {}) {
const filterBy = Object.entries(filter).reduce(
(acc, [key, value]) =>item => item[key] === value && acc(item),
() =>true
)
return items.filter(filterBy)
}
// usageconst items = [{BossId:'03',DateOfBirth:'1966-09-27T00:00:00',FamilyName:'Montejano',Gender:'Unspecified',GivenName:'Trinh',Id:'08',Title:'Tech Manager'},{BossId:'00',DateOfBirth:'1927-01-29T00:00:00',FamilyName:'Fetzer',Gender:'Unspecified',GivenName:'Winfred',Id:'00',Title:'CEO'},{BossId:'01',DateOfBirth:'1927-08-20T00:00:00',FamilyName:'Dandrea',Gender:'Male',GivenName:'Erich',Id:'02',Title:'VP of Marketing'},{BossId:'01',DateOfBirth:'1929-02-07T00:00:00',FamilyName:'Nisbet',Gender:'Male',GivenName:'Reinaldo',Id:'03',Title:'VP of Technology'},{BossId:'01',DateOfBirth:'1932-06-13T00:00:00',FamilyName:'Bufford',Gender:'Unspecified',GivenName:'Alleen',Id:'04',Title:'VP of HR'},{BossId:'02',DateOfBirth:'1936-09-26T00:00:00',FamilyName:'Klopfer',Gender:'Female',GivenName:'Kristyn',Id:'05',Title:'Director of Marketing'},{BossId:'01',DateOfBirth:'1937-11-23T00:00:00',FamilyName:'Duhon',Gender:'Male',GivenName:'Sophie',Id:'01',Title:'Tech Manager'},{BossId:'03',DateOfBirth:'1948-04-05T00:00:00',FamilyName:'Mirabal',Gender:'Female',GivenName:'Suanne',Id:'07',Title:'Tech Manager'},{BossId:'04',DateOfBirth:'1966-10-13T00:00:00',FamilyName:'Maslowski',Gender:'Unspecified',GivenName:'Norah',Id:'09',Title:'Tech Manager'},{BossId:'06',DateOfBirth:'1967-08-25T00:00:00',FamilyName:'Redford',Gender:'Female',GivenName:'Gertrudis',Id:'10',Title:'Tech Lead'},{BossId:'06',DateOfBirth:'1968-12-26T00:00:00',FamilyName:'Tobey',Gender:'Male',GivenName:'Donovan',Id:'11',Title:'Tech Lead'},{BossId:'09',DateOfBirth:'1969-10-16T00:00:00',FamilyName:'Vermeulen',Gender:'Male',GivenName:'Rich',Id:'12',Title:'Trainer Lead'},{BossId:'09',DateOfBirth:'1972-10-16T00:00:00',FamilyName:'Knupp',Gender:'Male',GivenName:'Santo',Id:'13',Title:'HR Manager'},{BossId:'12',DateOfBirth:'1974-03-23T00:00:00',FamilyName:'Grooms',Gender:'Female',GivenName:'Jazmin',Id:'14',Title:'Trainer'},{BossId:'13',DateOfBirth:'1978-08-25T00:00:00',FamilyName:'Cheeks',Gender:'Female',GivenName:'Annelle',Id:'15',Title:'Recruiter'},{BossId:'15',DateOfBirth:'1979-08-21T00:00:00',FamilyName:'Harshaw',Gender:'Unspecified',GivenName:'Eliza',Id:'16',Title:'Trainer'},{BossId:'08',DateOfBirth:'1980-02-09T00:00:00',FamilyName:'Broaddus',Gender:'Unspecified',GivenName:'Xiomara',Id:'17',Title:'Senior Software Developer'},{BossId:'11',DateOfBirth:'1981-09-08T00:00:00',FamilyName:'Jungers',Gender:'Unspecified',GivenName:'Erminia',Id:'18',Title:'Software Developer'},{BossId:'10',DateOfBirth:'1984-03-18T00:00:00',FamilyName:'Moffatt',Gender:'Female',GivenName:'Maria',Id:'19',Title:'Software Developer'},{BossId:'10',DateOfBirth:'1990-09-24T00:00:00',FamilyName:'Grimaldo',Gender:'Female',GivenName:'Tammera',Id:'20',Title:'Senior Software Developer'},{BossId:'10',DateOfBirth:'1992-06-18T00:00:00',FamilyName:'Das',Gender:'Female',GivenName:'Sharyl',Id:'21',Title:'Software Developer'},{BossId:'08',DateOfBirth:'1993-11-15T00:00:00',FamilyName:'Harlan',Gender:'Unspecified',GivenName:'Shan',Id:'22',Title:'UI Developer'},{BossId:'11',DateOfBirth:'1997-03-23T00:00:00',FamilyName:'Almeida',Gender:'Female',GivenName:'Mariah',Id:'23',Title:'QA Tester'},{BossId:'11',DateOfBirth:'1998-11-10T00:00:00',FamilyName:'Kerfien',Gender:'Male',GivenName:'Darnell',Id:'24',Title:'QA Tester'},{BossId:'11',DateOfBirth:'2004-04-22T00:00:00',FamilyName:'Vierra',Gender:'Female',GivenName:'Janell',Id:'25',Title:'QA Tester'}]
console.log(filterItemsBy(items, { BossId: '03' }))
Solution 3:
You can use filter (as mentioned by djfdev) or create a function by yourself like this
functionprovideID (mainItem, id) {
let arrayData = [] //We will push data to this array for (let i=0; i<mainItem.length; i++) {
if (Number(mainItem[i]["BossId"]) === id) arrayData.push(mainItem[i])
}
return arrayData;
}
console.log(provideID(items, 10))
here mainItem is your data array object and Id is the BossID corresponding to which you need to have data
Just keep in mind that you are most likely going to pass integer and your BossID is a string so you need to convert one of them.
Above, I have converted BossID to number. if you don't need that, you can just do
if (mainItem[i]["BossId"] === id) arrayData.push(mainItem[i])
Post a Comment for "Mapping Javascript Object Property With Another Property In Same Javascript Object"