如何使用嵌套数组在javascript中形成对象,使用第一个数组作为对象,数组的其余部分作为值

我得到了一个这样的数组:


data = [

  ['first_name', 'second_name', 'sex'],

  ['Jacob', 'David', 'M'],

  ['Kathryn', 'Gardener', 'F'],

  ['Jamaal', 'Dave', 'Unknown']

].

解决方案是将数组作为看起来像这样的对象返回:


[

  { first_name: "Jacob", second_name: "David", sex: "M" },

  { first_name: "Kathryn", second_name: "Gardener", sex: "F" }

  {....}

];

我写了以下代码:


var object = {};

var arrays = [];

for (i = 0; i < data.length; i++){

   for (var j = i; j < data.length; j++){

       var keys = data[0][i];

       var values = data [j][i];

       object[keys] = values;

   }

   arrays.push(object);

};

但我的输出是:


[

   { 

      first_name :'Jamaal',

      second_name : 'Dave',

      sex : 'unknown'

   },

   { 

      first_name :'Jamaal',

      second_name : 'Dave',

      sex : 'unknown'

   },

   {

      first_name :'Jamaal',

      second_name : 'Dave',

       sex : 'unknown'

   },

   {

      first_name :'Jamaal',

      second_name : 'Dave',

      sex : 'unknown'

    }

];

我也尝试了几种方法,要么我最终只能创建一个对象......


桃花长相依
浏览 156回答 3
3回答

慕尼黑的夜晚无繁华

您需要在第一个循环中使用新对象(引用),否则您会为每一行获得相同的对象。var data = [ ['first_name', 'second_name', 'sex'], ['Jacob', 'David', 'M'], ['Kathryn', 'Gardener', 'F'], ['Jamaal', 'Dave', 'Unknown']],&nbsp; &nbsp; result = [],&nbsp; &nbsp; i, j,&nbsp; &nbsp; object;for (i = 0; i < data.length; i++) {&nbsp; &nbsp; object = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// take a new object&nbsp; &nbsp; for (j = 0; j < data[i].length; j++) { // start from zero&nbsp; &nbsp; &nbsp; &nbsp; object[data[0][j]] = data[i][j];&nbsp; &nbsp;// use j as last key&nbsp; &nbsp; }&nbsp; &nbsp; result.push(object);}console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }一个更短的方法Object.fromEntries。const&nbsp; &nbsp; getObjects = ([header, ...data]) => data.map(values =>&nbsp; &nbsp; &nbsp; &nbsp; Object.fromEntries(values.map((value, index) => [header[index], value])));var data = [['first_name', 'second_name', 'sex'], ['Jacob', 'David', 'M'], ['Kathryn', 'Gardener', 'F'], ['Jamaal', 'Dave', 'Unknown']],&nbsp; &nbsp; result = getObjects(data);console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

慕斯709654

&nbsp; var data = [&nbsp; ['first_name', 'second_name', 'sex'],&nbsp; ['Jacob', 'David', 'M'],&nbsp; ['Kathryn', 'Gardener', 'F'],&nbsp; ['Jamaal', 'Dave', 'Unknown']]function createObject(arr){&nbsp; &nbsp; var arr2 = [];&nbsp; &nbsp; keysArr = arr[0];&nbsp; &nbsp; arr.splice(0,1);&nbsp; &nbsp; arr.forEach(function(x,i){&nbsp; &nbsp; &nbsp; &nbsp; arr2[i] = {};&nbsp; &nbsp; &nbsp; &nbsp; arr[i].forEach(function(y,j){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr2[i][keysArr[j]] = arr[i][j];&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; return arr2;}createObject(data);

陪伴而非守候

你可以做这样的事情dataToObj=(data)=>{&nbsp; &nbsp;let head=data[0];&nbsp; &nbsp;let result=[];&nbsp; &nbsp;for(let i=1;i<data.length;i++){&nbsp; &nbsp; &nbsp; let obj={};&nbsp; &nbsp; &nbsp; &nbsp;for(let j=0;j<head.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;obj[head[j]]=data[i][j]&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;result.push(obj);&nbsp;}&nbsp;return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript