2

I have the following array

[
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  }
]

I have an array in this format, where the work.id field in some corners is null. I try to do the ordering as follows ...

array.sort((a, b) => {
  return (
    a.work.id - b.work.id
  )
})

However, I get an error for the non-existence of the id

4
  • 1
    what should happen without? Commented Oct 15, 2020 at 14:17
  • Does this answer your question? Test for existence of nested JavaScript object key Commented Oct 15, 2020 at 14:19
  • Where should items without work ids be in the sort order? You could test for their existence and if not present, use Number.MAX_VALUE to make them sort after everything else of Number.MIN_VALUE to sort before everything else. Or use a second property as a secondary sort. Commented Oct 15, 2020 at 14:24
  • Is your question (a) how to check for the existence of a property, (b) how to sort if a property doesn't exist (in which case it's up to you--is a missing property "greater" or "less" than an existing property?, or (c) something else? Commented Oct 15, 2020 at 14:24

3 Answers 3

5

You could take optional chaining operator ?. with the properties and the Nullish coalescing operator ?? for taking a default value which respects zero.

By taking Number.MAX_VALUE as default value, all objects with missing nested objects are sorted to bottom.

const
    data = [{ id: 1, name: "Ruan Duarte", idade: 11, work: { id: 2 } }, { id: 2, name: "Raul Dias", idade: 13 }, { id: 7, name: "Caio", idade: 60, work: { id: 4 } }, { id: 3, name: "Felipe Lima", idade: 55 }, { id: 4, name: "Camila", idade: 25, work: { id: 3 } }];

data.sort((a, b) => (a?.work?.id ?? Number.MAX_VALUE) - (b?.work?.id ?? Number.MAX_VALUE));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

This might help

array.sort((a, b) => {
  return (
      (a.work != null ? a.work.id : -Infinity) - (b.work != null ? b.work.id : -Infinity) 
  )
});

Comments

0

If you need to sort by work.id:

const arr = [
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  }
]

arr.sort((a, b) => {
    if (a.work && b.work) {
    return a.work.id > b.work.id ? 1 : - 1
  } else if (!a.work && !b.work) {
        return a.id > b.id ? 1 : - 1
    } else if (!a.work) {
        return 1
    } else {
    return - 1
  }
})

output:

[
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  }
]

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.