将单个对象从JSON数组映射到新的JSON列表。

时间:2018-09-13 14:34:19

标签: javascript arrays angularjs object properties

我有一个对象的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条记录的进程将如何处理它列出不是数组。

2 个答案:

答案 0 :(得分:1)

您仅在此处变换array元素,而没有选择正确的object

您需要做的是先使用 .filter()method 过滤 array,然后使用 {{3 }}

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属性,可以使用 .map() method 对以下属性进行检查: filter()回调中的每个迭代对象:

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);

答案 1 :(得分:0)

使用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