如何通过ID从数组中选择对象?

时间:2019-01-24 11:34:38

标签: javascript lodash

我有一个对象,其中包含一个称为“申请人”的数组:

const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}

如何通过id从entities.applicants数组返回项目?

我尝试过类似的事情:

const applicantsById = persons.entities.applicants.find(a => a.id ===id)

console.log(applicantsById.get('16671520041'))

但是我不知道如何传递ID?

codepen

5 个答案:

答案 0 :(得分:2)

applicantsById必须是在id上参数化的函数

const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}

const applicantsById = id => persons.entities.applicants.find(a => a.id ===id)
console.log(applicantsById('16671520041'))

答案 1 :(得分:2)

使用原始javascript

在设置id变量之前,您需要定义applicantsById是什么。这样,您可以在对象数组中找到与您定义的id相等的id

const id = '16671520041',
applicantsById = persons.entities.applicants.find(a => a.id === id);

如果您希望将其与多个ID一起使用,可以将其扩展为一个函数:

const persons = {"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}},
getApplicantById = _id => persons.entities.applicants.find(({id}) => id === _id);

console.log(getApplicantById('16671520041')); // Abouli obj
console.log(getApplicantById('16671520039')); // Purdy obj

或者,使用 Lodash和ES6

const id = "16671520041";
res = _.find(persons.entities.applicants, {id}); // Abouli obj

答案 2 :(得分:0)

为此使用过滤器

const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}

console.log(get(16671520039));

function get(id)
{
return persons.entities.applicants.filter((e)=>e.id===id)
}

答案 3 :(得分:0)

您可以先定义ID,然后再进行find

const id = '16671520041';
const applicantsById = persons.entities.applicants.find(a => a.id ===id)

或者您可以定义一个接受id作为参数的函数。其他答案涵盖了如何使用现代ES6箭头功能执行此操作。如果您不熟悉它们,这是编写它的ES5函数声明方式。在这里,我还将人员数据传递给了函数。

const persons={"entities":{"applicants":[{"lastName":"Agamemnon","isPrimaryApplicant":false,"id":"16671520038"},{"lastName":"Purdy","isPrimaryApplicant":true,"id":"16671520039"},{"lastName":"Brekky","isPrimaryApplicant":true,"id":"16671520040"},{"lastName":"Abouli","isPrimaryApplicant":true,"id":"16671520041"}]}}

// Accept persons data, and an id
function getApplicantById(persons, id) {

  // Return the found applicant from the function
  return persons.entities.applicants.find(a => a.id ===id);
}

// Pass in the data, and the id to the function
const applicant = getApplicantById(persons, '16671520041');
console.log(applicant);

答案 4 :(得分:0)

您应该创建一个函数。

const applicantsById = (id) => persons.entities.applicants.find(a => a.id === id)
applicantsById('16671520041')

这将返回您所需的结果