1

I want rearrange the key value pair in a javascript object based on the value.

  var in = {
      'b': 'asdsad',
      'm': [{
          '0': 'asdsad'
      }, {
          '1': 'masdas'
      }, {
          '4': 'dsfdsfsdf'
      }],
      'c': 'masdas',
      'a': [{
          'b': 'asdsad'
      }, {
          'c': 'masdas'
      }, {
          'k': 'dsfdsfsdf'
      }],
      'z': 'asdasdads'
  }

I want to rearrange the object so that I will have the arrays as the last key value pairs.

Expected output

var out = {
    'b': 'asdsad',
    'c': 'masdas',
    'z': 'asdasdads',
    'm': [{
        '0': 'asdsad'
    }, {
        '1': 'masdas'
    }, {
        '4': 'dsfdsfsdf'
    }],
    'a': [{
        'b': 'asdsad'
    }, {
        'c': 'masdas'
    }, {
        'k': 'dsfdsfsdf'
    }]
}
0

2 Answers 2

1

According to the JavaScript specification's definition of Object:

It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

If you want to access the keys in an ordered fashion, you should use a structure that supports ordering, like an Array.

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

Comments

1

Hi Javascript sort based on value can only be done if there are fields with respective to it datastructure... below example sorts array with field value pls check out. Sort array of objects by string property value in JavaScript

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.