1

I have an array containing objects which contains an array also containing objects. When the object in the array inside the array of objects contains a value of 111 I want to return the whole object inside the first array.

The code below shows one example object in the first array.

[  
   {  
      "id":11,
      "bookingNumber":"1210",
      "arrivalDate":"2018-09-17T22:00:00.000Z",
      "departureDate":"2019-09-18T22:00:00.000Z",
      "customerId":2,
      "fields":[
          {  
             "value":"111",
             "display_name":"RoomNumber"
          },
          {  
             "value":"otherValue",
             "display_name":"PersonInfo"
          }
      ]
    }
 ]

My try which returns undefined:

const guest = guestGroups.forEach(function (guestGroup) {
             guestGroup.fields.filter(function (fields) {
                  if (fields.value === roomNumber)
                  return guestGroup;
            });
        });

1 Answer 1

1

You need to check whether some of the items in the fields array have the value you're looking for:

const valueToFind = '111';
const arr=[{"id":11,"bookingNumber":"1210","arrivalDate":"2018-09-17T22:00:00.000Z","departureDate":"2019-09-18T22:00:00.000Z","customerId":2,"fields":[{"value":"111","display_name":"RoomNumber"},{"value":"otherValue","display_name":"PersonInfo"}]}]

const foundObject = arr.find(
  ({ fields }) => fields.some(
    ({ value }) => value ===  valueToFind
  )
);
console.log(foundObject);

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

Comments

Your Answer

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