使用javascript使用循环将此数组设置为逗号分隔对象

时间:2018-06-13 05:45:52

标签: javascript node.js

var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
];

我有上面的数组,我想要像{

这样的东西
  "0": {
    "Postcode": "CF",
    "Detached Avg": "93177",
    "Detached Sales": "280",
    "Semi-detached Avg": "56037",
    "Semi-detached Sales": "179",
    "Terraced Avg": "49039",
    "Terraced Sales": "147",
    "Flat Avg": "48792",
    "Flat Sales": "140",
    "Overall Avg": "67238",
    "Total Sales": "746",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "1": {
    "Postcode": "CH",
    "Detached Avg": "86963",
    "Detached Sales": "11",
    "Semi-detached Avg": "48811",
    "Semi-detached Sales": "9",
    "Terraced Avg": "40850",
    "Terraced Sales": "2",
    "Flat Avg": "41500",
    "Flat Sales": "2",
    "Overall Avg": "65025",
    "Total Sales": "24",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "2": {
    "Postcode": "HR",
    "Detached Avg": "0",
    "Semi-detached Avg": "0",
    "Detached Sales": "0",
    "Semi-detached Sales": "0",
    "Terraced Avg": "71500",
    "Terraced Sales": "1",
    "Flat Avg": "0",
    "Flat Sales": "0",
    "Overall Avg": "71500",
    "Total Sales": "1",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "3": {
    "Postcode": "LD",
    "Detached Avg": "70927",
    "Detached Sales": "33",
    "Semi-detached Avg": "53489",
    "Semi-detached Sales": "10",
    "Terraced Avg": "0",
    "Terraced Sales": "0",
    "Flat Avg": "0",
    "Flat Sales": "0",
    "Overall Avg": "66872",
    "Total Sales": "43",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "4": {
    "Postcode": "LL",
    "Detached Avg": "72727",
    "Detached Sales": "144",
    "Semi-detached Avg": "50913",
    "Semi-detached Sales": "59",
    "Terraced Avg": "47330",
    "Terraced Sales": "30",
    "Flat Avg": "42193",
    "Flat Sales": "21",
    "Overall Avg": "62136",
    "Total Sales": "254",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "5": {
    "Postcode": "NP",
    "Detached Avg": "87351",
    "Detached Sales": "243",
    "Semi-detached Avg": "50333",
    "Semi-detached Sales": "120",
    "Terraced Avg": "38132",
    "Terraced Sales": "68",
    "Flat Avg": "26833",
    "Flat Sales": "6",
    "Overall Avg": "68696",
    "Total Sales": "437",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "6": {
    "Postcode": "SA",
    "Detached Avg": "84966",
    "Detached Sales": "168",
    "Semi-detached Avg": "50014",
    "Semi-detached Sales": "120",
    "Terraced Avg": "42617",
    "Terraced Sales": "66",
    "Flat Avg": "72081",
    "Flat Sales": "30",
    "Overall Avg": "65758",
    "Total Sales": "384",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  },
  "7": {
    "Postcode": "SY",
    "Detached Avg": "62147",
    "Detached Sales": "61",
    "Semi-detached Avg": "49425",
    "Semi-detached Sales": "37",
    "Terraced Avg": "39044",
    "Terraced Sales": "37",
    "Flat Avg": "60587",
    "Flat Sales": "12",
    "Overall Avg": "53002",
    "Total Sales": "147",
    "Type": "New",
    "Period": "1995",
    "Region": "WALES"
  }
}

6 个答案:

答案 0 :(得分:0)

我不打扰" 0":{}," 1":{}。只需创建一个[{},{}]的对象数组,因为它可以自动索引为objectarray [0]等



var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
];

var keys = data[0], newArr=[];
for (var i=1;i<data.length;i++) {
  var obj = {};
  for (var j=0;j<keys.length;j++) {
    obj[keys[j]]=data[i][j];
  }
  newArr.push(obj); 
   // or if you insist make newArr a {} and do newArr[""+(i-1)]=obj;
}
console.log(newArr);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以这样做。

zipObj接受两个数组,一个带有属性名,第二个带有实际值,并将它们拉成一个新对象。

&#13;
&#13;
var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
];

const zipObj = (arr1, arr2, obj) => {
  if (arr1.length === 0 || arr2.length === 0) { return; }
  obj[arr1[0]] = arr2[0];
  return zipObj(arr1.slice(1), arr2.slice(1), obj);
}

const res = [];

for (let i = 1; i < data.length; i++) {
  let obj = {};
  zipObj(data[0], data[i], obj);
  res.push(obj);
}

console.log(res);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以复制第一个元素并将其存储在变量中。使用slice从元素1浅层复制数组。使用map循环遍历数组。使用reduceObject.assign制作对象。

&#13;
&#13;
var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
];


var header = data[0];
var result = data.slice(1).map(o => o.reduce((c, v, i) =>Object.assign(c, {[header[i]]: v}), {}));

console.log(result);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用Array.shift获取包含keys.forEach的第一个项目,以循环显示其余内容并对其进行转换:

var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
]

var keys = data.shift()
var result = []

data.forEach(e => {
  var tmp = {}
  e.forEach((x, i) => tmp[keys[i]] = x)
  result.push(tmp)
})

console.log(result)

或递归函数:

var data = [
  ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
  ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
  ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
  ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
  ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
  ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
  ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
  ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
  ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
]

var keys = data.shift()
var result = []

function transform(arr) {
  var curr = arr.shift()
  var tmp = {}

  if (!tmp || !curr) return

  curr.forEach((x, i) => tmp[keys[i]] = x)
  
  result.push(tmp)
  transform(arr)
}

transform(data)

console.log(result)

答案 4 :(得分:0)

试试这段代码:

var data = [
    ["Postcode", "Detached", "Sales", "Semi-det", "Sales", "Terraced", "Sales", "Flat/mais", "Sales", "Overall average", "Total sales", "Type", "Period", "Region"],
    ["CF", "93177", "280", "56037", "179", "49039", "147", "48792", "140", "67238", "746", "New", "1995", "WALES"],
    ["CH", "86963", "11", "48811", "9", "40850", "2", "41500", "2", "65025", "24", "New", "1995", "WALES"],
    ["HR", "0", "0", "0", "0", "71500", "1", "0", "0", "71500", "1", "New", "1995", "WALES"],
    ["LD", "70927", "33", "53489", "10", "0", "0", "0", "0", "66872", "43", "New", "1995", "WALES"],
    ["LL", "72727", "144", "50913", "59", "47330", "30", "42193", "21", "62136", "254", "New", "1995", "WALES"],
    ["NP", "87351", "243", "50333", "120", "38132", "68", "26833", "6", "68696", "437", "New", "1995", "WALES"],
    ["SA", "84966", "168", "50014", "120", "42617", "66", "72081", "30", "65758", "384", "New", "1995", "WALES"],
    ["SY", "62147", "61", "49425", "37", "39044", "37", "60587", "12", "53002", "147", "New", "1995", "WALES"]
];
// create array key: "Postcode", "Detached", "Sales"...
var dataKey = data[0];
// remove first item
data.splice(0, 1);
// create array output
var dataOut = [];
// loop data
data.forEach(function(item) {
    // create obj json
    var jsonVariable = {};
    // loop array key
    dataKey.forEach(function(key, index) {
        // set key - value
        jsonVariable[key] = item[index];
    });
    // add obj json to array output
    dataOut.push(jsonVariable);
});
console.log('dataOut', dataOut);

答案 5 :(得分:-1)

声音简单:

my_object = {};
for (var i=0; i < data.length; i++){
  if (i == 0){// this is our keys
    var my_keys = data[i];
  } else {
    my_object[i] = {};
    for (var j=0; j < data[i].length; j++){
      my_object[i][my_keys[j]] = data[i][j];
    }
  }
}

没有测试代码,但你明白了。