Hetaxi Patel
Hetaxi Patel

Reputation: 17

How to iterate through variable of variable provided in array in jquery?

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'
    },

    {
    .
    .
    }
}];

Upvotes: 0

Views: 60

Answers (5)

Daniel Frech
Daniel Frech

Reputation: 335

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

Upvotes: 0

Adrian
Adrian

Reputation: 8597

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

Upvotes: 2

brk
brk

Reputation: 50291

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)

Upvotes: 0

barbsan
barbsan

Reputation: 3458

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)

Upvotes: 0

ciphrd
ciphrd

Reputation: 91

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
}

Upvotes: 0

Related Questions