根据对象数据将单独的数组创建到另一个数组中

时间:2019-06-26 10:05:24

标签: javascript

假设我有这个空数组:

var list = [];

这里是一个问题:如果我有data数组,其中填充了6个这样的对象:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

如何像这样用list数组的数据填充data数组:

var list = [
  ["Jack", "Sara", "Nick", "Doe", "Lee", "Sasha"],
  ["average", "good", "not bad", "good", "excellent", "good"]
];

我没有运气就把它用于循环:

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);

10 个答案:

答案 0 :(得分:8)

只需使用Array.prototype.map即可,它是根据现有数组创建新数组的,其结果数组的长度将与原始数组完全相同:

const data = [{ Name: "Jack", accuracy: "average", accent: 79, weight: 1 },{ Name: "Sara", accuracy: "good", accent: 24, weight: 2 },{ Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 },{ Name: "Doe", accuracy: "good", accent: 88, weight: 4 },{ Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 },{ Name: "Sasha", accuracy: "good", accent: 42, weight: 5 }]

const list = [data.map(x=>x.Name), data.map(x=>x.accuracy)];

console.log(list);

答案 1 :(得分:2)

更改list的声明以包含要推送到的子数组。

然后您的代码可以正常工作

var list = [[],[]];


var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);

答案 2 :(得分:2)

尝试使用reduce

 
var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

const list = data.reduce((acc,ele)=>{
 acc[0].push(ele.Name);
 acc[1].push(ele.accuracy);
 return acc
}, [[], []])

console.log(list);

答案 3 :(得分:1)

这将引发错误,因为您的列表数组为空,并且其中没有数组可用于推送值。

像这样初始化列表数组:

var list = [
  [],
  []
];

查看完整的代码:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

var list = [
  [],
  []
];

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}

console.log(list);

答案 4 :(得分:1)

尝试一下

const list = [];
const names= data.map(d=>d.Name);
list.push(names);
const val = data.map(d=>d.accuracy);
list.push(val);

答案 5 :(得分:1)

尝试使用map运算符:

list.push(data.map(n => n.Name), data.map(n => n.accuracy));
console.log(list);

答案 6 :(得分:1)

您可以通过如下所示的for循环进行操作:

securityManager.realms = $myRealm 

我个人会使用地图:

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

var list = [[],[]];

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);

答案 7 :(得分:1)

以下是reduce的示例:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

console.log(data.reduce((p,c) => p[0].push(c.Name) && p[1].push(c.accuracy) && p,[[],[]]))

答案 8 :(得分:1)

data.reduce((acc, r) => ([[...acc[0], r.name], [...acc[1], r.accuracy]]), [[], []])

答案 9 :(得分:0)

请参阅下文;在您的初始代码中,您尝试使用var list = [];,因为该数组未初始化,因此不会被填充,并且会给您未定义的内容;

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

// initialize the empty array(s) since it will get error when null / undefined;
var list = [ [], [] ];


for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}

console.log(list);