Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating separate arrays into another array from data of an object

Tags:

javascript

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);
like image 895
Sara Ree Avatar asked Jun 26 '19 10:06

Sara Ree


6 Answers

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);
like image 122
connexo Avatar answered Oct 12 '22 00:10

connexo


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);
like image 22
charlietfl Avatar answered Oct 11 '22 22:10

charlietfl


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);
like image 1
void Avatar answered Oct 11 '22 23:10

void


Try this

const list = [];
const names= data.map(d=>d.Name);
list.push(names);
const val = data.map(d=>d.accuracy);
list.push(val);
like image 1
akshay kishore Avatar answered Oct 11 '22 23:10

akshay kishore


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);
like image 1
dbramwell Avatar answered Oct 11 '22 22:10

dbramwell


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);
like image 1
Ghoul Ahmed Avatar answered Oct 12 '22 00:10

Ghoul Ahmed