0

I have an application where I want to be able to render a stack of content based on an orderId value in the object.

{
    "hero": {
        "body": "test body",
        "orderId": 1
    },
    "journey": {
        "body": "test body",
        "orderId": 0
    },
    "banner": {
        "body": "test body",
        "orderId": 2
    }
}
  • create an array of the keys based on the orderId in the object ["Journey", "Hero", "Banner"]
  • be able to order this object by orderId

I've tried something like this for the creating the array

Object.entries(content).sort(function(a, b) {
  return a[1].orderId - b[1].orderId;
});
The Old County
  • 89
  • 13
  • 59
  • 129
  • If you just want the ordered keys, you can use `Object.keys` like: `Object.keys(content).sort((a,b) => content[a].orderId - content[b].orderId)` – adiga Jun 16 '21 at 05:02
  • ok - but what about "be able to order this object by orderId" – – The Old County Jun 16 '21 at 08:51
  • If you want to create a new object, you can pass the sorted entries from your code to: `Object.fromEntries(sortedEntries)` – adiga Jun 16 '21 at 08:54

2 Answers2

3

The only part that you missing is to get the key and you can get it by using the map on the sorted array and retrieve the first element from this array.

sortedData.map((a) => a[0])

const content = {
  hero: {
    body: "test body",
    orderId: 1,
  },
  journey: {
    body: "test body",
    orderId: 0,
  },
  banner: {
    body: "test body",
    orderId: 2,
  },
};

const result = Object.entries(content)
  .sort((a, b) => a[1].orderId - b[1].orderId)
  .map((a) => a[0]);

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
0
let json = {
    "hero": {
      "body": "test body",
      "orderId": 1
    },
    "journey": {
      "body": "test body",
      "orderId": 0
    },
    "banner": {
      "body": "test body",
      "orderId": 2
    }
  }
  let arr = [];
  Object.entries(json).forEach(ele => {
    let keyword = ele[0];
    id = ele[1].orderId;
    arr[id] = keyword;
  })
  console.log(arr)
Sanmeet
  • 1,191
  • 1
  • 7
  • 15
  • what if there is one more entry in data `test: { body: "test test", orderId: 7, }` then you will get result as `[ 'journey', 'hero', 'banner', <4 empty items>, 'test' ]` which OP doesn't want. – DecPK Jun 16 '21 at 04:38
  • @decpk Nope it will get result something like. [ 'journey', 'hero', 'banner', 'test' ] skips 4 one instead giving no defined by the orderId.. so basically 4 will not exists .... I think you wanted this as said in the question orderId must define the index ! – Sanmeet Jun 16 '21 at 04:39
  • Try running this ! – Sanmeet Jun 16 '21 at 04:42
  • ok - but what about "be able to order this object by orderId" – The Old County Jun 16 '21 at 08:48
  • @The Old County That's impossible ! and of no use since you always access the object properties with keys (like object.key ) and it doesn't matter which number they get ... Kind of interesting right ! .... Also I really don't understand the meaning of asking this – Sanmeet Jun 16 '21 at 10:38