Let's say I have this empty array:
var list = [];
Here is the question: If I have data
array that is filled with 6 objects like this:
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 };
How can I populate the list
array with the data of data
array like this:
var list = [
["Jack", "Sara", "Nick", "Doe", "Lee", "Sasha"],
["average", "good", "not bad", "good", "excellent", "good"]
];
I have used this for loop with no luck:
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);
Simply use Array.prototype.map
which is designed for creating a new array from an existing one, where the resulting array will have exactly the same length as the original array:
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);
Change declaration of list
to include the sub arrays you want to push to.
Then your code works fine
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);
It will throw an error because your list array is empty and there is no array in it to push a value.
Initialize the list array like:
var list = [
[],
[]
];
See the complete code:
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);
Try this
const list = [];
const names= data.map(d=>d.Name);
list.push(names);
const val = data.map(d=>d.accuracy);
list.push(val);
You can do it with a for loop by like the below:
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);
Personally I would use map:
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 names = data.map(item => item.Name)
const accuracies = data.map(item => item.accuracy)
const list = [names, accuracies]
console.log(list);
Try this with 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With