如何遍历jQuery中数组中提供的变量的变量?

时间:2018-10-11 08:29:44

标签: javascript 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'
    },
    .
    .
    .
    {
    .
    .
    }
}];

想要这种格式的数据:

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

    {
    .
    .
    }
}];

5 个答案:

答案 0 :(得分:2)

您可以使用以下内容(只要已解决JSON响应中的语法问题):

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

答案 1 :(得分:0)

“迭代数组中的变量”是什么意思?

您写入的两个数据都是相同的,如果您想循环遍历数组中的项目,则可以这样做:

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
}

答案 2 :(得分:0)

您可以使用Array#map方法和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)

答案 3 :(得分:0)

使用数组map将创建一个新数组并返回对象

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)

答案 4 :(得分:0)

在@ Adriani6的答案的基础上,执行以下操作相同,但如果需要避免使data1数组保持不变以避免副作用,则保持不变:

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