Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON array sort by key value

I want to sort a a json array by selected key value.

    var Json = [{
        "post_id": 3,
        "distance": "2.130122"
    }, {
       "post_id": 3,
        "distance": null
    }, {
       "post_id": 4,
        "distance": "4.130122"
    }, {
       "post_id": 5,
        "distance": "3.130122"
    }];

I want to remove the index who have distance:null and want to sort it by distance order. expected json I want like this

 {
        "post_id": 3,
        "distance": "2.130122"
    }, {
       "post_id": 5,
        "distance": "3.130122"
    }, {
       "post_id": 4,
        "distance": "4.130122"
    }
and want to display only post_id as comma seperated **3,5,4**

Here I tried below code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var Json = [{
        "post_id": 3,
        "distance": "2.130122"
    }, {
       "post_id": 3,
        "distance": null
    }, {
       "post_id": 4,
        "distance": "4.130122"
    }, {
       "post_id": 5,
        "distance": "3.130122"
    }];
Json.sort((a,b)=> (a.distance > b.distance ? 1 : -1))
        var visiblePostId = Json
            .filter(function (item) {
                if (!isNaN(item.distance)) {
                    return item.post_id;
                }
            })
            .map((item) => {
                return item.post_id;
            })
            .join(',');
        console.log(visiblePostId)

<!-- begin snippet: js hide: false console: true babel: false -->

Extra help: If somehow duplicate post_id come after sort How can I remove duplicate post id?

>Solution :

You can do what you want with 4 steps:

  • filter on whether distance is null
  • sort based on distance (note converting to numeric is not strictly necessary as JS will automatically do that when using the - operator on two strings (see the spec))
  • map to the post_id
  • join with ,
var Json = [{
  "post_id": 3,
  "distance": "2.130122"
}, {
  "post_id": 3,
  "distance": null
}, {
  "post_id": 4,
  "distance": "4.130122"
}, {
  "post_id": 5,
  "distance": "3.130122"
}];

const VisiblePostIds = Json
  .filter(({ distance }) => distance !== null)
  .sort((a, b) => a.distance - b.distance)
  .map(({ post_id }) => post_id)
  .join(',')

console.log(VisiblePostIds)

Note that if you might have distance strings which are empty that you also want to filter out, you can change the filter to:

.filter(({ distance }) => distance)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading