1

I have two arrays of objects


   const reference_array= [{"key":"oAuthAccessToken","label":"Access Token","value":""},{"key":"clientId","label":"Client ID","value":""},{"label":"Verification Token","key":"verificationToken","value":""},{"label":"User ID","key":"userId","value":""},{"key":"signingSecret","label":"Signing Secret","value":""},{"label":"App ID","key":"appId","value":""},{"label":"Team ID","key":"teamId","value":""},{"label":"Name","key":"name","value":""},{"label":"Client Secret","key":"clientSecret","value":""},{"label":"ID","key":"id","value":""},{"label":"Channel ID","key":"channelId","value":""},{"key":"","value":""}]

   const resArray = [{"key":"3111","value":"12111"},{"key":"656556","value":"55666664"},{"key":"oAuthAccessToken","value":"123"},{"key":"clientId","value":"5"},{"key":"webhook_URL","value":"https://api.slack.com/1.1/account_activity/all/dev/webhooks.json?ref=7364616106833455"},{"key":"verificationToken","value":"55525"},{"key":"userId","value":"254"},{"key":"createdDate","value":"2019-09-05T07:47:24Z"},{"key":"createdBy","value":"John"},{"key":"webhook_ID","value":"7364616106833455"},{"key":"signingSecret","value":"12476233"},{"key":"appId","value":"9886"},{"key":"teamId","value":"8955653563"},{"key":"name","value":"56565"},{"key":"clientSecret","value":"656665656"},{"key":"id","value":"5656565"},{"key":"channelId","value":"55655565"}]

I would like to sort the resArray based on order of reference_array, the similar things of the two arrays is key value.

How to sort the resArray based on order of reference_array.

I tried in many ways but i have no luck.please help me to resolve the issue like below

Thanks in advance

resArray.sort(function(a, b) {
        return reference_array.map(function(x) {return x.key; }).indexOf(b) - reference_array.map(function(x) {return x.key; }).indexOf(a);
});

Expected Output :


const resArray = [{
        "key": "oAuthAccessToken",
        "value": "123"
    }, {
        "key": "clientId",
        "value": "5"
    }, {
        "key": "verificationToken",
        "value": "55525"
    }, {
        "key": "userId",
        "value": "254"
    }, {
        "key": "signingSecret",
        "value": "12476233"
    }, {
        "key": "appId",
        "value": "9886"
    }, {
        "key": "teamId",
        "value": "8955653563"
    }, {
        "key": "name",
        "value": "56565"
    }, {
        "key": "clientSecret",
        "value": "656665656"
    }, {
        "key": "id",
        "value": "5656565"
    }, {
        "key": "channelId",
        "value": "55655565"
    }, {
        "key": "3111",
        "value": "12111"
    }, {
        "key": "656556",
        "value": "55666664"
    }, {
        "key": "webhook_URL",
        "value": "https://api.slack.com/1.1/account_activity/all/dev/webhooks.json?ref=7364616106833455"
    }, {
        "key": "createdDate",
        "value": "2019-09-05T07:47:24Z"
    }, {
        "key": "createdBy",
        "value": "John"
    },
    {
        "key": "webhook_ID",
        "value": "7364616106833455"
    }
]
4
  • 2
    Sorting on which keys, Please put the expected outcome Commented Sep 5, 2019 at 10:51
  • Reference array keys Commented Sep 5, 2019 at 10:53
  • i updated expected output in question @NeelRathod Commented Sep 5, 2019 at 11:00
  • How is the reference_array sorted? If it is sorted on "key" then you should be able to sort resArray the same way. What if they get out of sync and one has "key" items that the other doesn't? Commented Sep 5, 2019 at 11:02

1 Answer 1

1

You could take an object as reference for the sorting order and take Infinity for unknown key for sorting this items to the bottom of the array.

var reference = [{ key: "oAuthAccessToken", label: "Access Token", value: "" }, { key: "clientId", label: "Client ID", value: "" }, { label: "Verification Token", key: "verificationToken", value: "" }, { label: "User ID", key: "userId", value: "" }, { key: "signingSecret", label: "Signing Secret", value: "" }, { label: "App ID", key: "appId", value: "" }, { label: "Team ID", key: "teamId", value: "" }, { label: "Name", key: "name", value: "" }, { label: "Client Secret", key: "clientSecret", value: "" }, { label: "ID", key: "id", value: "" }, { label: "Channel ID", key: "channelId", value: "" }, { key: "", value: "" }],
    array = [{ key: "3111", value: "12111" }, { key: "656556", value: "55666664" }, { key: "oAuthAccessToken", value: "123" }, { key: "clientId", value: "5" }, { key: "webhook_URL", value: "https://api.slack.com/1.1/account_activity/all/dev/webhooks.json?ref=7364616106833455" }, { key: "verificationToken", value: "55525" }, { key: "userId", value: "254" }, { key: "createdDate", value: "2019-09-05T07:47:24Z" }, { key: "createdBy", value: "John" }, { key: "webhook_ID", value: "7364616106833455" }, { key: "signingSecret", value: "12476233" }, { key: "appId", value: "9886" }, { key: "teamId", value: "8955653563" }, { key: "name", value: "56565" }, { key: "clientSecret", value: "656665656" }, { key: "id", value: "5656565" }, { key: "channelId", value: "55655565" }],
    order = reference.reduce((o, { key }, i) => (o[key] = i + 1, o), {});

array.sort((a, b) => (order[a.key] || Infinity) - (order[b.key] || Infinity));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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.