我有一个JavaScript对象,我很期待map()
是Id
,而另一个id
在同一个JavaScript对象中,我的JavaScript对象纲要是
var 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"
}
];
我想创建一个函数,当该函数提供BossId
时,该函数将输出向该老板报告的员工列表。例如,BossId
'03'
的输出为
[
{
"BossId": "03",
"DateOfBirth": "1966-09-27T00:00:00",
"FamilyName": "Montejano",
"Gender": "Unspecified",
"GivenName": "Trinh",
"Id": "08",
"Title": "Tech Manager"
},
{
"BossId": "03",
"DateOfBirth": "1948-04-05T00:00:00",
"FamilyName": "Mirabal",
"Gender": "Female",
"GivenName": "Suanne",
"Id": "07",
"Title": "Tech Manager"
}
]
我尝试过的事情:
items.map(res => ({
...res.Users,
Users: items
.filter(id => id.Id == id.BossId)
.map(({ Id, ...rest }) => rest)
}))
答案 0 :(得分:1)
您应该使用filter
而不是map
。 Map始终会创建一个与原始数组相同长度的数组,而过滤会生成一个子集:
function getItemsByBossId (items, id) {
return items.filter(item => item.BossId === id)
}
答案 1 :(得分:1)
您可以使用过滤器(如djfdev所述)或自行创建函数
function provideID (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))
此处 mainItem是您的数据数组对象,Id是您需要数据所对应的BossID
请记住,您很有可能要传递整数,并且您的 BossID是字符串,因此您需要转换其中之一。
以上,我已将BossID转换为数字。如果不需要的话,就可以
if (mainItem[i]["BossId"] === id) arrayData.push(mainItem[i])
答案 2 :(得分:0)
您可以更进一步,为任何属性(或其组合)创建通用过滤函数,如下所示:
function filterItemsBy (items, filter = {}) {
const filterBy = Object.entries(filter).reduce(
(acc, [key, value]) => item => item[key] === value && acc(item),
() => true
)
return items.filter(filterBy)
}
// usage
const 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' }))