-1

I have the json response which I want to make the order based on the label .Is it possible to arrange the json response data based on the stander fixed order.want to arrang in the order defined any required json let order={"data":["test1","test2","test3","test4","test5","test6","test7","test8"]}(text may vary).Is it possible to arrange in order

let order={"data":["test1","test2","test3","test4","test5","test6","test7","test8"]}
const input = {
  "data_report": [{
      "data": [1, 2, 0, 3],
      "label": "user2",
      "backgroundColor": "blue"
    },
    {
      "data": [3, 4, 2, 5],
      "label": "test3",
      "backgroundColor": "#a3eaae"
    },
    {
      "data": [2, 3, 1, 4],
      "label": "test4",
      "backgroundColor": "#37bd11"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "test7",
      "backgroundColor": "#43bee3"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "user5",
      "backgroundColor": "#a3eaae"
    },
    {
      "data": [0, 1, 0, 2],
      "label": "test6",
      "backgroundColor": "#1195bd"
    },
    {
      "data": [0, 1, 0, 2],
      "label": "test1",
      "backgroundColor": "#aeb5b7"
    },
    {
      "data": [1, 2, 0, 3],
      "label": "test7",
      "backgroundColor": "pink"
    }
  ],
  "weeks": ["Week 1 ", "Week 2 ", "Week 3 ", "Week 4 "]
}
var sorted = input.data_report.sort((item)=>{return order.data;});
console.log( sorted);
<div class="test"></div>
user3386779
  • 6,883
  • 20
  • 66
  • 134
  • 4
    There is no JSON in this question. JSON is a text format. What you have in `input` is an object with two properties, both of which are arrays. – Heretic Monkey May 26 '20 at 14:21
  • do you mean that you want the output to be, `{test1,user2,test3,test4,user5,test6,test,test8}`? I'm not sure that I got your question. – ROOT May 26 '20 at 14:29
  • please my edited post and snippet – user3386779 May 26 '20 at 14:30
  • 1
    @user3386779, can you edit the question, and add the output you want, its not clear what the output should be? – ROOT May 26 '20 at 14:34
  • Does this answer your question? [How do I sort an array of objects based on the ordering of another array?](https://stackoverflow.com/questions/9755889/how-do-i-sort-an-array-of-objects-based-on-the-ordering-of-another-array) – Heretic Monkey May 26 '20 at 14:35

3 Answers3

3

You can do that with sort:

input = { "data_report": [{ "data": [1, 2, 0, 3], "label": "Test2", "backgroundColor": "blue" }, { "data": [3, 4, 2, 5], "label": "test3", "backgroundColor": "#a3eaae" }, { "data": [2, 3, 1, 4], "label": "test4", "backgroundColor": "#37bd11" }, { "data": [1, 2, 0, 3], "label": "test7", "backgroundColor": "#43bee3" }, { "data": [1, 2, 0, 3], "label": "test5", "backgroundColor": "#a3eaae" }, { "data": [0, 1, 0, 2], "label": "test6", "backgroundColor": "#1195bd" }, { "data": [0, 1, 0, 2], "label": "test1", "backgroundColor": "#aeb5b7" }, { "data": [1, 2, 0, 3], "label": "test7", "backgroundColor": "pink" } ], "weeks": ["Week 1 ", "Week 2 ", "Week 3 ", "Week 4 "]};

input.data_report = input.data_report.sort((a,b)=>a.label.match(/\d+/)[0]-b.label.match(/\d+/)[0]);

console.log(input);
gorak
  • 5,233
  • 1
  • 7
  • 19
  • please see my updated expected string.I don't want to arrange in alphabetical order. .I need it in defined set of json as I mentioned – user3386779 May 26 '20 at 14:17
  • @user3386779, you can definitely do that.I'll edit my answer. – gorak May 26 '20 at 14:22
  • 1
    @user3386779 this is the correct answer is not ordering alphabetically its ordering first by the last decimal digit in the string so it's solving your problem, vote this answer – David Torroija May 26 '20 at 14:23
1

Yes you can use .sort method on the array

var sorted = input.data_report.sort((item)=>{return item.label;});

Something along those lines.

Simon
  • 736
  • 6
  • 21
0

You need the JSON to be sorted before appending to HTML. I suggest something like sort function.

_.map(_.sortBy(json, 'key'), 'value'); (this one using lodash)