0

I planed to prepare this array by separate those kind of array with index specification

Default data format

[{
      "Emp_code": "EM-00001",
      "Emp_title": "11",
      "Emp_firstName": "22",
      "Emp_lastName": "33",
      "Emp_dateOfBirth": "20-10-1985",
      "Con_title": "title",
      "Con_email": "email",
      "Con_addres": "address",
      "Con_phone": "phone"
    }]

Wanted format

[{
  "emp": {
      "code": "EM-00001",
      "title": "11",
      "firstName": "22",
      "lastName": "33",
      "dateOfBirth": "20-10-1985",
  },
  "con": {
      "Con_title": "title",
      "Con_email": "email",
      "Con_addres": "address",
      "Con_phone": "phone"
  }
}]
3
  • 1
    Ok and what have you tried? It's not a super complicated requirement, you can loop through the properties, building a new object along the way. Check if the key prefix exists as a key on the object you're building and if not add it, then add the key/value pair as a sub-property (minus the key prefix). At the moment you're just stating your requirements, SO is not a code writing service. Commented Oct 3, 2018 at 6:02
  • You mean there can be any number of formats such as "emp" , "con" ,.. but only seperate these by key -> "con" by "Con_title" "emp" by "Emp_code"? Commented Oct 3, 2018 at 6:08
  • I'm going to separate based on array object prefix, current we have only two but it could flexible if it contain more prefixes. anyway I have tried many time with above mention or login but I just starter with typescript Commented Oct 3, 2018 at 6:10

3 Answers 3

2

You can reduce the property names to a starting accumulator of [{ emp: {} }, { con: {} }] and each iteration you can add the property to the corresponding item in the accumulator.

const data = [{
      "Emp_code": "EM-00001",
      "Emp_title": "11",
      "Emp_firstName": "22",
      "Emp_lastName": "33",
      "Emp_dateOfBirth": "20-10-1985",
      "Con_title": "title",
      "Con_email": "email",
      "Con_addres": "address",
      "Con_phone": "phone"
    }];
    
const format = obj =>
  Object.getOwnPropertyNames(obj[0]).reduce(
    (acc, prop) => {
      if (prop.startsWith('Emp_')) {
        acc[0].emp[prop.replace('Emp_', '')] = obj[0][prop];
      } else {
        acc[1].con[prop] = obj[0][prop];
      }
      return acc;
    },
    [{ emp: {} }, { con: {} }]
  );
 
 console.log(format(data));

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

Comments

1

var item = {
  "Emp_code": "EM-00001",
  "Emp_title": "11",
  "Emp_firstName": "22",
  "Emp_lastName": "33",
  "Emp_dateOfBirth": "20-10-1985",
  "Con_title": "title",
  "Con_email": "email",
  "Con_addres": "address",
  "Con_phone": "phone"
}
var data = [item, item];

var res = []

for (var item of data) {
  var temp = {};
  res.push(temp);
  for (var prop in item) {
    var parts = prop.split('_');
    var prefix = parts[0].toLowerCase();
    temp[prefix] = temp[prefix] || {};
    temp[prefix][prefix === 'emp' ? parts[1] : prop] = item[prop]
  }
}

console.log(res);

Comments

1

Below script checks element using loops, then splits them into prefix & suffix. Then checks for whether prefix is present in resulting array or not. If it's not then adds that prefix into array & prepares the result.

var a = [{"Emp_code": "EM-00001", "Emp_title": "11", "Emp_firstName": "22", "Emp_lastName": "33", "Emp_dateOfBirth": "20-10-1985", "Con_title": "title", "Con_email": "email", "Con_addres": "address", "Con_phone": "phone"}];
var b = [];
$.each(a, function(arrKey, arrData){
    var tempArr = {};
    $.each(arrData, function(key, value){
        var arrKey = key.split('_');
        var prefix = arrKey[0];
        var suffix = arrKey[1];
        if( $.inArray(prefix, Object.keys(tempArr)) == -1 ) {
            tempArr[prefix] = {};
        }
        tempArr[prefix][suffix]=value;
    });
    b.push(tempArr);
});
console.log(b);

2 Comments

Any text to explain what is happening?
Edited an answer.

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.