Abhishek
Abhishek

Reputation: 567

Sort JSON by pre-defined set of keys in javascript

Lets suppose I have a JSON ListA:

LIST A

[
  {
    "address": "wellington lane",
    "age": "23",
    "country": "Australia",
    "name": "Mike",
    "profession": "Lawyer"
  },
  {
    "address": "Street 25",
    "age": "26",
    "country": "New Zealand",
    "name": "Parks",
    "profession": "Engineer"
  },
  {
    "address": "North cross",
    "age": "29",
    "country": "Korea",
    "name": "Wanda",
    "profession": "Doctor"
  }
]

LIST B

["name","age","address","country","profession"]

The requirement is I need to sort the JSON LIST A according to the array LIST B and the output should look like:

[
  {
    "name": "Mike",
    "age": "23",
    "address": "wellington lane",
    "country": "Australia",
    "profession": "Lawyer"
  },
  {
    "name": "Parks",
    "age": "26",
    "address": "Street 25",
    "country": "New Zealand",
    "profession": "Engineer"
  },
  {
    "name": "Wanda",
    "age": "29",
    "address": "North cross",
    "country": "Korea",
    "profession": "Doctor"
  }
]

How can I sort this out? I have tried this solution, but it seems this is not working.

Upvotes: 0

Views: 63

Answers (4)

Giovanni Esposito
Giovanni Esposito

Reputation: 11156

Ciao, you could try to iterate in both arrays and create an array result with ordered attributes like this:

let input = [
      {
        "address": "wellington lane",
        "age": "23",
        "country": "Australia",
        "name": "Mike",
        "profession": "Lawyer"
      },
      {
        "address": "Street 25",
        "age": "26",
        "country": "New Zealand",
        "name": "Parks",
        "profession": "Engineer"
      },
      {
        "address": "North cross",
        "age": "29",
        "country": "Korea",
        "name": "Wanda",
        "profession": "Doctor"
      }
    ]

    let fields = ["name","age","address","country","profession"]
    
    let result = [];
    
    input.forEach(el => {
       let resultobj = {}
       fields.forEach(fi => {
          resultobj[fi] = el[fi]          
       })
       result.push(resultobj)
    })
    
    console.log(result)

Upvotes: 1

Elise Chant
Elise Chant

Reputation: 5196

Where "LIST A" is listA and "LIST B" is listB:

const formatList = () => {
    return listA.map(item => {
        const res = {};
        listB.map(label => {
            res[label] = item[label];
        })
        return res;
    })
};

console.log(formatList())

Upvotes: 0

eol
eol

Reputation: 24565

One way to do this without having to rely on an external library:

const arr = [
  {
    'address': 'wellington lane',
    'age': '23',
    'country': 'Australia',
    'name': 'Mike',
    'profession': 'Lawyer',
  },
  {
    'address': 'Street 25',
    'age': '26',
    'country': 'New Zealand',
    'name': 'Parks',
    'profession': 'Engineer',
  },
  {
    'address': 'North cross',
    'age': '29',
    'country': 'Korea',
    'name': 'Wanda',
    'profession': 'Doctor',
  },
];
const keyOrder = ['name', 'age', 'address', 'country', 'profession'];

function applyKeyOrder(obj, keyOrder) {
  const res = {};
  for (const key of keyOrder) {
    res[key] = obj[key];
  }
  return res;
}

const result = arr.map(element => applyKeyOrder(element, keyOrder));
console.log(result);

Note that this assumes that all keys in the keyOrder-array actually exist in the objects, i.e. fields would get lost if they're not present in the array.

Upvotes: 1

Hao Wu
Hao Wu

Reputation: 20724

Here's a solution using map and reduce:

const arr = [
  {
    "address": "wellington lane",
    "age": "23",
    "country": "Australia",
    "name": "Mike",
    "profession": "Lawyer"
  },
  {
    "address": "Street 25",
    "age": "26",
    "country": "New Zealand",
    "name": "Parks",
    "profession": "Engineer"
  },
  {
    "address": "North cross",
    "age": "29",
    "country": "Korea",
    "name": "Wanda",
    "profession": "Doctor"
  }
];

const order = ["name", "age", "address", "country", "profession"];

const newArr = arr.map(e => order.reduce((obj, key) => ({ ...obj, [key]: e[key] }), {}));

console.log(newArr);

Upvotes: 1

Related Questions