1

I have a JSON Array of objects like:

$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'}
];

but I need to extract a single record (and specific fields) to its own list based on a value, in this case the id.

I've used .map before, but that will make a new array of all records but lets me specify field, I'm not aware of a way I can filter at this point

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

I'm not sure how to extract one record based on a single value, as in my case the id will always be unique, but if its not how would a process expecting 1 record handle it when I need it as a single list not an array.

3 Answers 3

1

You are only transforming the array elements here, without selecting the right object.

What you need to do is to filter the array first using .filter()method, then transform the result with .map() method:

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

Demo:

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

If the id is not unique:

If the id is not unique, you will need to check over the whole object properties, you can do that using .every()method over the properties of each iterated object inside filter() callback:

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, '')
    });
 });

Demo:

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

Sign up to request clarification or add additional context in comments.

3 Comments

Why does he needs to map it?
because hew wants only specific fields from the object as he said.
I didn't pay attention about the specific fields (it was in bracket - like optional)
0

You can iterate JSON array and filter by ID as below -

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

If the ID is not unique then you will get multiple records in array with same ID and you should put business logic/validation to handle/reject it.

Comments

0

What about using 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);

Otherwise you can use the angularjs $filter

  • PS 1: I use simple var (not $scope so you can see result in snipet).
  • PS 2: If you wan't also to return specifics fields so you will have to map the return value (refer to chŝdk's answer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.