从数组对象数组中检索数据

时间:2017-03-30 02:10:50

标签: json node.js

在我的应用程序中,我试图返回在所有项目中工作的员工的javascript对象。我的数据数组如下:

   var data = [
{
    projectName: "project1", projectId: 1,
    employees: [
        {fullName: "John Doe", employeeId: 1},
        {fullName: "Jane Smith", employeeId: 2}
    ]
},

{
    projectName: "project2", projectId: 2,
    employees: [
        {fullName: "John Doe", employeeId: 1},
        {fullName: "Mary Jones", employeeId: 3},
        {fullName: "Bill Evans", employeeId: 4}
    ]
}

];

我需要搜索employeeid 1并返回两个项目。我如何搜索它。

4 个答案:

答案 0 :(得分:0)

我希望这是您正在寻找的代码:

for(i=0;i<data.length;i++)
 {
    emp=data[i].employees;
    for(j=0;j<emp.length;j++)
    {
        if(emp[j].employeeId==1)
          console.log('Project of' + emp[j].fullName + 'is = ' + data[i].projectName)
    }
}

答案 1 :(得分:0)

尝试使用Array.filterArray.findIndex

<强> UPD: 由于Array.findIndex是ES6中的一项新功能,某些浏览器可能不支持,但它有一个不受支持的浏览器使用的polyfill,请参阅文档here)。

function doFilter(arr) {
  return arr.filter(function(item) {
    return item.employees.findIndex(function(employee) {
      return employee.employeeId === 1;
    }) > -1;
 });
}

此代码段将返回目标项目数组,其中包含ID为1的员工。

var data = [{
    projectName: "project1",
    projectId: 1,
    employees: [{
        fullName: "John Doe",
        employeeId: 1
      },
      {
        fullName: "Jane Smith",
        employeeId: 2
      }
    ]
  },
  {
    projectName: "project2",
    projectId: 2,
    employees: [{
        fullName: "John Doe",
        employeeId: 1
      },
      {
        fullName: "Mary Jones",
        employeeId: 3
      },
      {
        fullName: "Bill Evans",
        employeeId: 4
      }
    ]
  },
  {
    projectName: "project3",
    projectId: 3,
    employees: [{
      fullName: "Jane Smith",
      employeeId: 2
    }]
  }
];

function doFilter(arr) {
  return arr.filter(function(item) {
    return item.employees.findIndex(function(employee) {
      return employee.employeeId === 1;
    }) > -1;
  });
}
console.log(doFilter(data));

答案 2 :(得分:0)

希望这很好用

&#13;
&#13;
var data = [{
    projectName: "project1",
    projectId: 1,
    employees: [{
        fullName: "John Doe",
        employeeId: 1
      },
      {
        fullName: "Jane Smith",
        employeeId: 5
      }
    ]
  },

  {
    projectName: "project2",
    projectId: 2,
    employees: [{
        fullName: "John Doe",
        employeeId: 1
      },
      {
        fullName: "Mary Jones",
        employeeId: 3
      },
      {
        fullName: "Bill Evans",
        employeeId: 4
      }
    ]
  }
];

function getEmployeeById(id) {
  var e = $.map(data, function(a) {
    var r = $.grep(a.employees, function(b) {
      return b.employeeId == id;
    });
    if (r.length) {
      console.log(a.projectName, r[0].fullName)
    }
  });
}
getEmployeeById(1)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

可以这么简单。只需迭代和比较。 https://jsfiddle.net/

for( i=0;i<data.length;i++)
{
    for( j=0;j<data[i].employees.length;j++)
  {
     if (data[i].employees[j].employeeId == 1)
      console.log(data[i].projectId);
  }
}