How to separate array of objects with specific array prefix in typescript

javascript map
javascript array to string without commas
array to string javascript
javascript join two arrays
array join java
typescript distinct array of objects
javascript add array to array
get specific property from array of objects typescript

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"
  }
}]

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);

Create array of unique objects by property, I'd probably use a flags object during the filtering, like this: var flags = {}; var newPlaces = places.filter(function(entry) { if (flags[entry.city]) { return false; }  1. Using a for loop and the slice function. Basically, every method will use the slice method in order to split the array, in this case what makes this method different is the for loop. In case that the array is not uniform, the remaining items will be in an array too, however the size will be less for obvious reasons.

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));

Array.join( ): concatenate array elements to form a string , An optional character or string used to separate one element of the array from the next a string into array elements—with the split( ) method of the String object. The user adds the data in the User interface, the controller gets the data in Array of the object as a model which we need to supply it REST API. So we need to convert this Array of objects to JSON objects. Read Data from Database. Angular application Consumes REST API data. This data is in the format of JSON format.

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);

Array.prototype.join(), The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by  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.

Array in TypeScript, An array is a special type of data type which can store multiple values of different data types Arrays can contain elements of any data type, numbers, strings, or even objects. includes(), Checks whether the array contains a certain element. You can also declare an array as an Array of some specific data type using a syntax that looks very much like C# generics. Like my previous example, this code also declares an array that can only hold Customer objects: var custs: Array<Customer>; You can also use TypeScript's union types to define an array that can hold several different types.

Javascript: Understanding Objects vs Arrays [Part 1], What are Objects & how do they differ from Arrays in Javascript? push multiple comma separated elements, in this case 3 strings. You don't need to prefix each object property like I did in the example One of the common operations we perform with Arrays is checking if a certain value is in the array. A common pattern in in Javascript is to cast arrays of objects into a separate object, with a certain property defined as the new object’s key. For example, instead of [{id: 'abfq-f39f', name: ‘Test’}] , you would have {‘abfq-f39f’: {id: 1, name: 'Test'}}

JavaScript, Difference Between, Digital Electronics & Logic Design, Divide and Conquer, Dynamic arr.filter() function is used to create a new array from a given array consisting of from the given array which satisfy a condition set by the argument function. How to compare two JavaScript array objects using jQuery/JavaScript ? By default, the JavaScript Array.sort function converts each element in the array that needs to be sorted into a string, and compares them in Unicode code point order. const foo = [9, 1, 4

Comments
  • 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.
  • 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"?
  • 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
  • Any text to explain what is happening?
  • Edited an answer.