0
var data1: [{
    {
        postalcode:'qwerty',
        cT: 23,
        latitude:57.232324,
        longitude: -2.343543,
        call_reason: 'xyz',
        Call_Sub_reason:'abc'
    },
    {
        postalcode:'qwerty1',
        cT: 43,
        latitude:57.223524,
        longitude: -1.435453,
        call_reason: 'xyz1',
        Call_Sub_reason:'abc1'
    },
    .
    .
    .
    {
    .
    .
    }
}];

want data in this format:

var data1 : [{
    {
        postalcode:'qwerty',
        cT: 23,
        location:[57.232324,-2.343543],
        call_reason: 'xyz',
        Call_Sub_reason:'abc'
    },

    {
        postalcode:'qwerty1',
        cT: 65,
        location:[58.232324,-1.343543],
        call_reason: 'xyz1',
        Call_Sub_reason:'abc1'
    },

    {
    .
    .
    }
}];
6
  • 2
    That's not a valid syntax. Commented Oct 11, 2018 at 8:32
  • If my data is in this format in database and I want data in the following mentioned type .. how can I get it? Commented Oct 11, 2018 at 8:35
  • The data format you provided is not correct, and wait a minute, are both format same ? I mean, you provided one format and expected it in the same format ? Commented Oct 11, 2018 at 8:37
  • As above: [{{ is how your JSON starts, that is invalid syntax. Also, keys should be quoted and double quotes should be used, not single quotes. Commented Oct 11, 2018 at 8:38
  • @Towkir A new key is used, which is a concatenation of longitude and latitude. Commented Oct 11, 2018 at 8:39

5 Answers 5

2

You can use the following (given you fix the syntax issue in your JSON response):

var formatted = data1.map(x => {
    var xy = Object.assign({}, x);

    xy.location = [xy.latitude, xy.longitude]
    delete xy.latitude;
    delete xy.longitude;

    return xy;
});

var items = [{ postalcode:'qwerty', cT: 23, latitude:57.232324, longitude: -2.343543, call_reason: 'xyz', Call_Sub_reason:'abc' }, { postalcode:'qwerty1', cT: 43, latitude:57.223524, longitude: -1.435453, call_reason: 'xyz1', Call_Sub_reason:'abc1' }];

var formatted = items.map(x => {
  var xy = Object.assign({}, x);
  
  xy.location = [xy.latitude, xy.longitude]
  delete xy.latitude;
  delete xy.longitude;
  
  return xy;
})

console.log(formatted);

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

3 Comments

Well, you beat me by 3 minutes so have in upvote in lieu of me deleting my identical answer. I was too busy getting distracted lol
Be arware, that "items.map" will change the "items" array as well and not only the new "formattedItems" array.
@DanielFrech Good spot. Corrected.
0

What do you mean by "iterate through variable of variable in array" ?

Both of the data you wrote are the same, if you meant that you would like to loop through the items in the array, you can do so like this:

es5

for (var i = 0; i < data1.length; i++) {
  var dataItem = data1[i];
  /**
   * dataItem will be
   * { postalcode:'qwerty', cT: 23, location:[57.232324,-2.343543], call_reason: 'xyz', Call_Sub_reason:'abc' },
   */
}

es6

for (let dataItem of data1) {
  // same
}

1 Comment

They're not the same... A new key is added called location which combines latitude and longitude.
0

You can use Array#map method and object destructuring

var arr = [{
        postalcode:'qwerty',
        cT: 23,
        latitude:57.232324,
        longitude: -2.343543,
        call_reason: 'xyz',
        Call_Sub_reason:'abc'
    },
    {
        postalcode:'qwerty1',
        cT: 43,
        latitude:57.223524,
        longitude: -1.435453,
        call_reason: 'xyz1',
        Call_Sub_reason:'abc1'
    }];
    
   var result =  arr.map(
   ({postalcode, cT, latitude, longitude, call_reason, Call_Sub_reason}) => ({postalcode, location: [ latitude, longitude], cT, call_reason, Call_Sub_reason})
   );
   console.log(result)

Comments

0

Use array map which will create a new array and return the object

var data1 = [{
    postalcode: 'qwerty',
    cT: 23,
    latitude: 57.232324,
    longitude: -2.343543,
    call_reason: 'xyz',
    Call_Sub_reason: 'abc'
  },
  {
    postalcode: 'qwerty1',
    cT: 43,
    latitude: 57.223524,
    longitude: -1.435453,
    call_reason: 'xyz1',
    Call_Sub_reason: 'abc1'

  }
];


let newArray = data1.map((item) => {
  return {
    postalcode: item.postalcode,
    cT: item.cT,
    location: item.location,
    call_reason: item.call_reason,
    Call_Sub_reason: item.Call_Sub_reason
  }
})
console.log(newArray)

Comments

0

Building on the answer of @Adriani6, the following does the same but leaves the data1 array unchanged if that is needed to avoid possible side effects:

const items = [{ postalcode:'qwerty', cT: 23, latitude:57.232324, longitude: -2.343543, call_reason: 'xyz', Call_Sub_reason:'abc' }, { postalcode:'qwerty1', cT: 43, latitude:57.223524, longitude: -1.435453, call_reason: 'xyz1', Call_Sub_reason:'abc1' }];

let formattedItems = items.map(item => {
  let newItem = {...item};
  newItem.location = [newItem.latitude, newItem.longitude];
  delete newItem.latitude;
  delete newItem.longitude;
  return newItem;
});

console.log(items);
console.log(formattedItems);

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.