0

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.

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?

4
  • filter first, then sort Commented Sep 4, 2022 at 3:29
  • That's not a "json array." It's just an array Commented Sep 4, 2022 at 3:29
  • 1
    What are you trying to achieve with the map and join? Commented Sep 4, 2022 at 3:30
  • @Nick I want to display comma separated post_id only Commented Sep 4, 2022 at 3:31

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Can you give me little help like if duplicate post_id come then How can I remove duplicate post_id in filter?
@LemonKazi there are some good examples of how do remove duplicates here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.