用JS查询JSON(使用未知父级)

时间:2013-04-22 21:22:08

标签: javascript json

Lets说我们有一个看起来像这样的对象。

    // first name - of employee 3
    company.employees.employeeId[3].first


    // last name - of employee 3
    company.employees.employeeId[3].last


    // department - of employee 3
    company.employees.employeeId[3].department

是否有方法/库来执行这样的查询

    // looking for all employees in department Z
    result = company.employees.[*].department['accounting']

1 个答案:

答案 0 :(得分:3)

你可以.filter()他们:

var result = company.employees.employeeID.filter(function(employee) {
    return employee.department === 'accounting';
});