【问题标题】:map single object from JSON array to new JSON list.将单个对象从 JSON 数组映射到新的 JSON 列表。
【发布时间】:2019-02-18 07:49:16
【问题描述】:

我有一个 JSON 对象数组,例如:

$scope.people = [
  {id: 1, forename: 'Bob', surname: 'Bobbington', DOB: '01/01/1990', address: '123 Fake St'},
  {id: 2, forename: 'Bill', surname: 'Billster', DOB: '01/12/1999', address: '56 Road'},
  {id: 3, forename: 'Sally', surname: 'Bobbington', DOB: '15/04/1987', address: '123 Fake St'},
  {id: 4, forename: 'Flerp', surname: 'Derp', DOB: '01/09/1991', address: '34 Derpington'}
];

但我需要根据一个值(在本例中为 id)将单个记录(和特定字段)提取到其自己的列表中。

我以前使用过 .map,但这会创建一个包含所有记录的新数组,但让我指定字段,我不知道此时我可以过滤的方式

specificFields = RawData.map(function (el) {
  return ({
    surname: el['surname'].replace(/ /g, ''),
    DOB: el['DOB'].replace(/ /g, '')
    })
 })

我不确定如何根据单个值提取一条记录,因为在我的情况下,id 将始终是唯一的,但如果不是,当我需要它作为单个记录时,期望 1 条记录的进程将如何处理它列出不是数组。

【问题讨论】:

    标签: javascript arrays angularjs object properties


    【解决方案1】:

    你这里只是变换array元素,没有选择正确的object

    您需要做的是首先使用 .filter()method 过滤 array,然后使用 .map() method 转换结果:

    const specificFields = RawData.filter(e => e.id === id).map(function (el) {
      return ({
        surname: el['surname'].replace(/ /g, ''),
        DOB: el['DOB'].replace(/ /g, '')
        });
     });
    

    演示:

    const RawData = [
      {id: 1, forename: 'Bob', surname: 'Bobbington', DOB: '01/01/1990', address: '123 Fake St'},
      {id: 2, forename: 'Bill', surname: 'Billster', DOB: '01/12/1999', address: '56 Road'},
      {id: 3, forename: 'Sally', surname: 'Bobbington', DOB: '15/04/1987', address: '123 Fake St'},
      {id: 4, forename: 'Flerp', surname: 'Derp', DOB: '01/09/1991', address: '34 Derpington'}
    ];
    
    const id = 2;
    
    const specificFields = RawData.filter(e => e.id === id).map(function (el) {
      return ({
        surname: el['surname'].replace(/ /g, ''),
        DOB: el['DOB'].replace(/ /g, '')
        });
     });
     
     console.log(specificFields);

    如果id 不是唯一的:

    如果id 不是唯一的,您将需要检查整个object 属性,您可以使用.every()method 来检查@987654335 中每个迭代对象的属性@回调:

    const specificFields = RawData.filter(e => Object.keys(e).every(k => e[k] == person[k])).map(function (el) {
      return ({
        surname: el['surname'].replace(/ /g, ''),
        DOB: el['DOB'].replace(/ /g, '')
        });
     });
    

    演示:

    const RawData = [
      {id: 1, forename: 'Bob', surname: 'Bobbington', DOB: '01/01/1990', address: '123 Fake St'},
      {id: 2, forename: 'Bill', surname: 'Billster', DOB: '01/12/1999', address: '56 Road'},
      {id: 3, forename: 'Sally', surname: 'Bobbington', DOB: '15/04/1987', address: '123 Fake St'},
      {id: 4, forename: 'Flerp', surname: 'Derp', DOB: '01/09/1991', address: '34 Derpington'}
    ];
    
    const person = {id: 4, forename: 'Flerp', surname: 'Derp', DOB: '01/09/1991', address: '34 Derpington'};
     
    const specificFields = RawData.filter(e => Object.keys(e).every(k => e[k] == person[k])).map(function (el) {
      return ({
        surname: el['surname'].replace(/ /g, ''),
        DOB: el['DOB'].replace(/ /g, '')
        });
     });
     
     console.log(specificFields);

    【讨论】:

    • 他为什么需要映射它?
    • 因为如他所说,他只想要对象的特定字段。
    • 我没有关注具体的字段(它在括号中 - 像可选的)
    【解决方案2】:

    使用filter怎么样?

        var people = [
          {id: 1, forename: 'Bob', surname: 'Bobbington', DOB: '01/01/1990', address: '123 Fake St'},
          {id: 2, forename: 'Bill', surname: 'Billster', DOB: '01/12/1999', address: '56 Road'},
          {id: 3, forename: 'Sally', surname: 'Bobbington', DOB: '15/04/1987', address: '123 Fake St'},
          {id: 4, forename: 'Flerp', surname: 'Derp', DOB: '01/09/1991', address: '34 Derpington'}
        ];
    
        let result = people.filter(person => person .id == 3);
        console.log(result);

    否则你可以使用angularjs $filter

    • PS 1:我使用简单的var(不是$scope,因此您可以在片段中看到结果)。
    • PS 2:如果您还不想返回具体字段,则必须映射返回值(请参阅chŝdk's answer

    【讨论】:

      【解决方案3】:

      您可以迭代 JSON 数组并按 ID 过滤,如下所示 -

      angular.forEach($scope.people, function(people) {
        if(people.id == filterID){
          // your code goes here
          console.log(people);
        }
      });
      

      如果 ID 不是唯一的,那么您将在数组中获得多个具有相同 ID 的记录,您应该使用业务逻辑/验证来处理/拒绝它。

      【讨论】:

        猜你喜欢
        • 2021-05-18
        • 1970-01-01
        • 2019-05-27
        • 2019-02-06
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-23
        相关资源
        最近更新 更多