1

I'm using .map from lodash as per below:

    map(data, (data: array) => {
              return (
                <FormItem label={data?.label}>
                  <FormControl fullWidth>
                    <Typography>{data?.value}</Typography>
                  </FormControl>
                </FormItem>
              );

From my understanding lodash .map does not guarantee iteration order, it means the order I will display won't necessarily be same from the original array. Is that correct? If so there are any other functions I could use to map it on the correct order?

  • 1
    Why would you assume it doesn't run in order? A quick glance at the source shows that it uses the default array map function under the hood if the collection is an array. – Evan Trimboli Jun 24 '21 at 02:33
  • `code` _.map({ 'a': 4, 'b': 8 }, square); // => [16, 64] (iteration order is not guaranteed) `code` https://lodash.com/docs/4.17.15#map They mention it on the documentation, maybe it is a misinterpretation from my side. – Antonio Guedes Jun 24 '21 at 02:40
  • 2
    That would only apply to object literals, since they don't (or perhaps now didn't, depending on the ES version you're using). Arrays will iterate in-order. See: https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties – Evan Trimboli Jun 24 '21 at 03:08
  • You can use mix of `sortBy` from https://lodash.com/docs/#sortBy or `orderBy` from https://lodash.com/docs/#orderBy – Goutham Jun 24 '21 at 05:33

0 Answers0