0

I am trying to retrieve values back in a nested array but my function is returning back everything. I would like to just back the 'carTime' values in my array.

What my array is returning.

[
  {
    "carId": "13122",
    "carInstances": [
      {
        "carInstanceId": "472",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
  {
    "carId": "1312",
    "carInstances": [
      {
        "carInstanceId": "47209",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
]

Lodash:

const result = [
  {
    "carId": "13122656",
    "carInstances": [
      {
        "carInstanceId": "47209",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
  {
    "carId": "1312",
    "carInstances": [
      {
        "carInstanceId": "4720",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
]

const findCategoryById = (sections) => {
        const jobInstance = _.forEach(sections, (section) => {
          return section.carInstances.carTime;
        });
        return jobInstance;
      };
      findCategoryById(result)

Desired Output:

2020-09-23T21:45:00.000+0000,
2020-09-23T21:45:00.000+0000

1 Answer 1

1

You do not need lodash for solving this problem. A small JS function will do with the use of reduce and map. With lodash you could do: _(result).flatMap('carInstances').flatMap("carTime")).values()

const result = [
  {
    "carId": "13122656-169f-45fa-be47-26c9d23dcb7b",
    "carInstances": [
      {
        "carInstanceId": "47209558-f9e1-4f81-a600-5da6ce238a6e",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
  {
    "carId": "13122656-169f-45fa-be47-26c9d23dcb7b",
    "carInstances": [
      {
        "carInstanceId": "47209558-f9e1-4f81-a600-5da6ce238a6e",
        "carTime": "2020-09-23T21:45:00.000+0000",
        "state": "COMPLETE",
      }
    ],
    "isPaused": false,
  },
];

const listCarTime = (res) => {
  return res.reduce((acc, car) => {
    acc.push(...car.carInstances.map((instance) => instance.carTime));
    return acc;  
  }, []);
}

console.log("lodash", _(result).flatMap('carInstances').flatMap("carTime").values())


console.log("vanilla", listCarTime(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

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

1 Comment

It would be helpful to explain to OP "I would like to just back the 'carTime'" is done via the .map(i => i.carTime) part of your answer.

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.